[#397974] Having difficulty processing text files - some techniques? — b1_ __ <lists@...>
I am trying to extract information from a po-ker hand history text file
On Fri, Aug 3, 2012 at 10:39 AM, b1_ __ <lists@ruby-forum.com> wrote:
[#397986] Why does it fail? def Module1::Module2.hello — Iñaki Baz Castillo <ibc@...>
Hi, the following code fails:
I know one way to define module method
2012/8/4 William Herry <william.herry.china@gmail.com>:
[#397987] OptionParser error handling (InvalidArgument) — Panagiotis Atmatzidis <ml@...>
Hello,
[#397988] Help with sqlite3 please — Kaye Ng <lists@...>
I'm on Windows 7 Ultimate, 64-bit
sqlite is not ruby, so you should look for a sqlite group ;)
> However it looks like you have 'SQL' at the beginning of your CREATE
I have a script that I wrote that uses nokogiri to parse some HTML, the results
[#398005] RubyInstaller — "Cale B." <lists@...>
Hello forum,
[#398025] unable to require 'test/unit' — Ja Tse <lists@...>
Hi All
[#398031] Gem install or usage problem in shared environment — Tom Moulton <lists@...>
I am moving to a Westhost shared CPanel account and I am trying to set
I got a solution from WestHost and it may help others:
Ryan Davis wrote in post #1071503:
You're running ruby 1.8 and requiring libraries without first using:
James Harrison wrote in post #1071601:
2012/8/8 Tom Moulton <lists@ruby-forum.com>:
[#398040] Linguistics gem — Dave Castellano <lists@...>
Have Linguistics gem in gem list:
[#398043] Redefining constants for a given instance only — "Andrea Dallera" <andrea@...>
Hello,=0A=0A=C2=A0 =C2=A0 let's say we have two empty classes:=0A=0Aclass=
On Sun, Aug 5, 2012 at 7:19 PM, Andrea Dallera <andrea@andreadallera.com> wrote:
=C2=A0Hi Robert,=0A=0A=C2=A0 =C2=A0 thanks for your reply.=C2=A0=0A=0A>=C2=
[#398054] how perform addition a text value with an integer — Fosiul Alam <lists@...>
HI
[#398063] Join with ActiveRecord using non-standard schema — Tedi Roca <lists@...>
Hi,
Tried with this:
On Mon, Aug 6, 2012 at 7:39 AM, T- Di <lists@ruby-forum.com> wrote:
Hassan Schroeder wrote in post #1071438:
On Mon, Aug 6, 2012 at 8:38 AM, T- Di <lists@ruby-forum.com> wrote:
[#398081] Gem install issues — Nathan Ahmed <elihu5991@...>
Hi,
Hi Nathan,
Actually, don't bother with that FAQ, try this instead: =
[#398092] Nesting and constants — Thomas Thomassen <lists@...>
module TT_Test
[#398122] Initializer with self — "Iwan B." <lists@...>
I have tho classes A and B1, and b is defined as:
[#398135] Help with database-related code pls — Kaye Ng <lists@...>
Hi guys! This is just a part of the code of a program that can load a
Kaye Ng wrote in post #1071689:
> Try adding after this line:
[#398145] Why if executes two times? — ajay paswan <lists@...>
If I set a breakpoint at if statement, I see that the statement gets
It shouldn't happened, may be due to IDE gives the illusion to you.
ZhanGNer wrote in post #1071763:
[#398148] File eliminating 'a' — ajay paswan <lists@...>
How can I eliminate every 'a' in a file if it is not inside double
[#398154] fasterCSV meaning of a line — ajay paswan <lists@...>
What is the meaning of line?:
[#398168] Time-efficient program — ajay paswan <lists@...>
Suppose you are given a program, now you are given a task to minimize
[#398185] Working with thread — ajay paswan <lists@...>
How can I ensure that the main child lives till all children finishes
[#398190] How do you order your class methods? — masta Blasta <lists@...>
Just getting some layout ideas from other fellow devs.
On Fri, Aug 10, 2012 at 5:03 PM, masta Blasta <lists@ruby-forum.com> wrote:
I generally use a top-down approach, where new methods are added
[#398191] Using Test::Unit::Testcase for multiple execution — Gerrit Leder <gerrit.leder@...>
Hello all,
[#398197] Unable to print a block argument. — Ja Tse <lists@...>
I am doing some experiment with ruby and I am not sure why the #len
Hi,
Thanks, JAn
[#398209] Delete a lines from a files — Fosiul Alam <lists@...>
Hi
Hi,
Jan E. wrote in post #1072045:
[#398239] What should I use instead of Generator in 1.9? — "Andrew S." <lists@...>
Hi,
[#398245] namespace instance methods? — John Doe <lists@...>
I have a large class with many instance methods that I want to
Thanks for the quick reply. I guess what I really want is an object
That does work, but it makes no sense, because you're creating a new
[#398251] Fastest way to get first line and ith line in a file? — ajay paswan <lists@...>
What is the Fastest way to get first line and ith line in a file?
[#398265] meaning of @variable@? — ajay paswan <lists@...>
what is the meaning of the following instruction
QmVjYXVzZSBpdCdzIGEgc3RyaW5nLCBhbmQgaW4gc2luZ2xlIHF1b3Rlcywgc3VyZWx5IGl0IHdv
[#398267] Desktop App to upload file to S3 — Avantec Van <lists@...>
Hi All,
Avantec Van wrote in post #1072190:
[#398281] delete a string from config — Fosiul Alam <lists@...>
hi i am trying to automate a Firewall config and i have a bellow line in
[#398287] Idea: def ... end returns the symbolized version of the newly-defined method, instead of nil — Peter <lumbergh@...>
This would allow useful syntax constructs such as this:
Not sure if you've ever worked on large classes in large Ruby/Rails
[#398289] Creating a C extension in Ruby — "Andy P." <lists@...>
I am trying to create a C extension for ruby. I am very new to
[#398324] Variable in string in array — Dave Castellano <lists@...>
Anybody know how to embed a #{} in a string and then place the string in
On Wed, Aug 15, 2012 at 2:46 PM, Dave Castellano <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1072441:
[#398329] Eval without eval? — Dipesh Gtm <lists@...>
Can anyone explain how "Proc.new{}" (String) could be converted into
[#398331] Print method — Fred McArthur <lists@...>
Hello everyone. I'm a newbie with a question whose answer is probably
[#398341] string processing — Fosiul Alam <lists@...>
Hi
sorry the code i wrote that does not do anything
[#398361] Why is assert_match deprecated and not assert_no_match? — "Rob N." <lists@...>
I have found assert_match useful in the past, but have noticed that it
[#398362] case vs if-else — ajay paswan <lists@...>
Which one is faster?
Dude, does it really matter if one variant is some microseconds faster
Hello,
So then what is the point of ruby if it's just for human-readability? Isn't
[#398366] extending class, namespace question — Ronnie Aa <lists@...>
Hi everybody,
[#398382] Methods with same name and parameters in a single class. — Rubyist Rohit <lists@...>
I saw a sample code of Ruby where in a class can have two methods with
[#398385] A Ruby class is never closed — Rubyist Rohit <lists@...>
Is it true that a Ruby class definition is never closed? Even after
I think he basically wants to have a class that can never ever again be
On Sat, Aug 25, 2012 at 11:36 PM, Marc Heiler <lists@ruby-forum.com> wrote:
On Mon, Aug 27, 2012 at 10:58 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
[#398400] variable from array to function to array — Dave Castellano <lists@...>
anyone know if it is possible to pass elements from an array (eg 20,50 )
Am 17.08.2012 02:39, schrieb Dave Castellano:
The bigger picture... I am saving multiple question templates in a
[#398425] Connection refused.again — Waldemar Renz <lists@...>
Hello,
[#398432] Default parameters? + (Noob) Guidance — incag neato <lists@...>
Hi guys, would love your help with a few questions:
On Fri, Aug 17, 2012 at 6:57 PM, incag neato <lists@ruby-forum.com> wrote:
[#398447] accessing protected or private methods from class methods — Denis Leclair <lists@...>
There is all kinds of information out there on Ruby method visibility
Hi,
Hi Jan,
On Sat, Aug 18, 2012 at 9:27 AM, Denis Leclair <lists@ruby-forum.com> wrote:
[#398456] Access returned value in a string? — Dave Castellano <lists@...>
Can I access the returned value in a string?
Additionally you could do something like
[#398469] TCPserver how to signal EOT — tammy roberts <lists@...>
Hi I am writing some networking code and am having a heck of a time
Hi Robert, it is me again, my computer crashed and I lost my login
On Mon, Aug 20, 2012 at 11:57 AM, tammy roberts2 <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1072878:
On Tue, Aug 21, 2012 at 4:17 AM, tammy roberts2 <lists@ruby-forum.com> wrote:
[#398475] How to get the minitest capture_io method found in my specs? — Milli Sami <lists@...>
This is my `spec/helper.rb` file:
[#398482] RPG online game — Axel Aguilar <lists@...>
Hi, I'm new to the forum so first I introduce myself a bit :D
[#398498] A Ruby Book (free to use) — Karthikeyan A k <lists@...>
Hello people, I have written a book on Ruby programming language.
[#398504] How to create an EXecutable file (Linux) — Fosiul Alam <lists@...>
Hi
Maybe you should try to use jRuby, I heard there is a possibility to
[#398506] Save a file by clicking on a link — ajay paswan <lists@...>
I clicked a link to download a file using ruby, now I see the open-save
I meant go look up the MIME type of the file you're having trouble with,
Almost everything can be done differently. You just have to tailor the
[#398507] Finding methods inside a scope — Dipesh Gtm <lists@...>
Suppose I have a scope
[#398530] Watir webdriver, IE on ubuntu — ajay paswan <lists@...>
I can run chrome and firefox, but I have no idea how to have IE on
[#398537] deleting directories matching a pattern — Thomas Luedeke <lists@...>
I cannot believe I'm having trouble with this - it should be utterly
[#398543] Is calling system() from a thread suppose to be safe? — rgs@...
Hey there,
[#398547] Add a line into a file at multiple locations above a existing line — "Aravind K." <lists@...>
Hi All,
[#398563] Parsing unix filenames from STDIN — David Jacobs <david@...>
How can I parse filenames read from STDIN via the standard Unix parsing rules (without duplicating said rules)? All of the following would be parsed as coherent file names:
On Sat, Aug 25, 2012 at 12:57 AM, David Jacobs <david@wit.io> wrote:
On Sat, Aug 25, 2012 at 11:51 AM, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:
[#398567] Webcam as a light sensor. — "Michał S." <lists@...>
How I can do that? I found one lib, but it's only for linux and I need
[#398587] Are jobs allowed? — John Cox <lists@...>
Am I allowed to post job openings here? I don't want to be rude. But I
On Sun, 26 Aug 2012 23:58:41 +0900
[#398630] nokogiri xpath query with text containing "&" — Mert <lists@...>
Hi all,
On Tue, Aug 28, 2012 at 1:04 PM, Mert <lists@ruby-forum.com> wrote:
> That's not true. The issue is with your understanding (or lack
On Tue, Aug 28, 2012 at 1:41 PM, Mert <lists@ruby-forum.com> wrote:
[#398641] force child threads run paralelly? — ajay paswan <lists@...>
I have created two child thread using main thread- child1 and child2.
Ruby version:
On Tue, Aug 28, 2012 at 7:19 AM, ajay paswan <lists@ruby-forum.com> wrote:
Tony Arcieri wrote in post #1073605:
Apart from getting the above answers.. I would like to point out that: =
Hi All,
On Tue, Aug 28, 2012 at 6:11 PM, Andriy Andreyev <andreev00@gmail.com> wrote:
Robert Klemme писал 28.08.2012 21:31:
[#398646] dynamic class resolution? — Gary Weaver <lists@...>
The following is going to start off sounding Rails-specific, but if you
[#398664] nokogiri/ruby and troublesome characters in url — Sybren Kooistra <lists@...>
I'm very new to using ruby, and I can't seem to figure something out
[#398668] Regex Non Greedy Match — Naga Vijay <lists@...>
Hello,
[#398676] What Really is Happening? — Pepe Pew <lists@...>
Hello, I need your help.
[#398684] Can I do this with Ruby and sqlite alone? — Kaye Ng <lists@...>
Hi guys.
Hi,
Jan E. wrote in post #1073735:
On 29.08.2012 09:41, Kaye Ng wrote:
Hi Alex,
On 29.08.2012 10:59, Kaye Ng wrote:
[#398689] Dir.mkdir appending question mark after directory name — Joakim Langvand <lists@...>
Hi all
On Wed, Aug 29, 2012 at 10:51 AM, Joakim Langvand <lists@ruby-forum.com> wr=
[#398696] working on multiple machines in a LAN — ajay paswan <lists@...>
I have a program, now I want this to be run on multiple computers
ajay paswan wrote in post #1073756:
[#398709] How to add a non standard Gem path? — Iñaki Baz Castillo <ibc@...>
Hi, I would like to install some Gems in a custom directory (i.e.
[#398715] Common Traps for C extensions — Bernhard Brodowsky <lists@...>
Hi, I am writing a toy library in C++ and currently, I am writing a Ruby
On Wed, Aug 29, 2012 at 8:08 PM, Bernhard Brodowsky
Robert Klemme wrote in post #1073869:
[#398717] String in rails and ruby show first n character — "Jean-Sébastien D." <lists@...>
I am just wondering how can i show the first 200 character of a string ?
[#398724] how do extract timezone from datetime object? — Dan Quach <lists@...>
I'm working on an external interface where a partner website is giving
On Wed, Aug 29, 2012 at 5:32 PM, Dan Quach <lists@ruby-forum.com> wrote:
[#398735] can't install neither original nor green shoes — "Sebastjan H." <lists@...>
Hi,
[#398745] IE-Watir-webDriver — ajay paswan <lists@...>
When I do:
[#398747] Qt signals driving me crazy — Damjan Rems <lists@...>
Below is source of basic window with TreeWidget on left and TabWidget on
On Friday 31 August 2012 Damjan Rems wrote
[#398753] Load an array of 10 ratings by position — Joao Silva <lists@...>
Hi all.
Re: Connection refused.again
Robert Klemme wrote in post #1072689:
> On Fri, Aug 17, 2012 at 5:44 PM, Waldemar Renz <lists@ruby-forum.com>
> wrote:
>> #else
>> # errno != 0 ? (Network.testresult(true)) :
>> (Network.testresult(false))
>> #end
>> end
>> end
>
> Well, if you comment the condition around the raise then of course
> every call to #check will trigger the exception. Is that your
> problem?
Unfortunately not. If it would work by removing the comments, or by
making the entire condition a comment, there should appear an error
regarding the line "$c.connect($aic)", because yesterday i got this far
and someone else got the same problem with last line, but not the one
troubling me at the moment. Instead I get the following answers:
First by making the problem to a comment, the program just hangs up.
Second by removing everything commented in SocketError I get the message
"Script 'Sockets' line 526:NameError occurred. uninitialized constant
SocketError::Network".
This seems to me, that I need to make Network an object from a class I
don't know and to make a method called testing. What should testing do?
As far I understand this method should work like a ping and by
successful pinging return the value 1 else 0, shouldn't it? For the case
this is not right, I paste here the script of the original author:
module Win32
def copymem(len)
buf = "\0" * len
Win32API.new("kernel32", "RtlMoveMemory", "ppl", "").call(buf, self,
len)
buf
end
end
# Extends the numeric class.
class Numeric
include Win32
end
# Extends the string class.
class String
include Win32
end
module Winsock
DLL = "ws2_32"
#-----------------------------------------------------------------------------
# * Accept Connection
#-----------------------------------------------------------------------------
def self.accept(*args)
Win32API.new(DLL, "accept", "ppl", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Bind
#-----------------------------------------------------------------------------
def self.bind(*args)
Win32API.new(DLL, "bind", "ppl", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Close Socket
#-----------------------------------------------------------------------------
def self.closesocket(*args)
Win32API.new(DLL, "closesocket", "p", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Connect
#-----------------------------------------------------------------------------
def self.connect(*args)
Win32API.new(DLL, "connect", "ppl", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Get host (Using Adress)
#-----------------------------------------------------------------------------
def self.gethostbyaddr(*args)
Win32API.new(DLL, "gethostbyaddr", "pll", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Get host (Using Name)
#-----------------------------------------------------------------------------
def self.gethostbyname(*args)
Win32API.new(DLL, "gethostbyname", "p", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Get host's Name
#-----------------------------------------------------------------------------
def self.gethostname(*args)
Win32API.new(DLL, "gethostname", "pl", "").call(*args)
end
#-----------------------------------------------------------------------------
# * Get Server (Using Name)
#-----------------------------------------------------------------------------
def self.getservbyname(*args)
Win32API.new(DLL, "getservbyname", "pp", "p").call(*args)
end
#-----------------------------------------------------------------------------
# * Convert Host Long To Network Long
#-----------------------------------------------------------------------------
def self.htonl(*args)
Win32API.new(DLL, "htonl", "l", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Convert Host Short To Network Short
#-----------------------------------------------------------------------------
def self.htons(*args)
Win32API.new(DLL, "htons", "l", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Inet Adress
#-----------------------------------------------------------------------------
def self.inet_addr(*args)
Win32API.new(DLL, "inet_addr", "p", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Inet N To A
#-----------------------------------------------------------------------------
def self.inet_ntoa(*args)
Win32API.new(DLL, "inet_ntoa", "l", "p").call(*args)
end
#-----------------------------------------------------------------------------
# * Listen
#-----------------------------------------------------------------------------
def self.listen(*args)
Win32API.new(DLL, "listen", "pl", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Recieve
#-----------------------------------------------------------------------------
def self.recv(*args)
Win32API.new(DLL, "recv", "ppll", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Select
#-----------------------------------------------------------------------------
def self.select(*args)
Win32API.new(DLL, "select", "lpppp", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Send
#-----------------------------------------------------------------------------
def self.send(*args)
Win32API.new(DLL, "send", "ppll", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Set Socket Options
#-----------------------------------------------------------------------------
def self.setsockopt(*args)
Win32API.new(DLL, "setsockopt", "pllpl", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Shutdown
#-----------------------------------------------------------------------------
def self.shutdown(*args)
Win32API.new(DLL, "shutdown", "pl", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Socket
#-----------------------------------------------------------------------------
def self.socket(*args)
Win32API.new(DLL, "socket", "lll", "l").call(*args)
end
#-----------------------------------------------------------------------------
# * Get Last Error
#-----------------------------------------------------------------------------
def self.WSAGetLastError(*args)
Win32API.new(DLL, "WSAGetLastError", "", "l").call(*args)
end
end
if !Object.const_defined?(:Socket) # for compatibility
#===============================================================================
# ** Socket - Creates and manages sockets.
#-------------------------------------------------------------------------------
# Author Ruby
# Version 1.8.1
#===============================================================================
class Socket
#-----------------------------------------------------------------------------
# * Constants
#-----------------------------------------------------------------------------
AF_UNSPEC = 0
AF_UNIX = 1
AF_INET = 2
AF_IPX = 6
AF_APPLETALK = 16
PF_UNSPEC = 0
PF_UNIX = 1
PF_INET = 2
PF_IPX = 6
PF_APPLETALK = 16
SOCK_STREAM = 1
SOCK_DGRAM = 2
SOCK_RAW = 3
SOCK_RDM = 4
SOCK_SEQPACKET = 5
IPPROTO_IP = 0
IPPROTO_ICMP = 1
IPPROTO_IGMP = 2
IPPROTO_GGP = 3
IPPROTO_TCP = 6
IPPROTO_PUP = 12
IPPROTO_UDP = 17
IPPROTO_IDP = 22
IPPROTO_ND = 77
IPPROTO_RAW = 255
IPPROTO_MAX = 256
SOL_SOCKET = 65535
SO_DEBUG = 1
SO_REUSEADDR = 4
SO_KEEPALIVE = 8
SO_DONTROUTE = 16
SO_BROADCAST = 32
SO_LINGER = 128
SO_OOBINLINE = 256
SO_RCVLOWAT = 4100
SO_SNDTIMEO = 4101
SO_RCVTIMEO = 4102
SO_ERROR = 4103
SO_TYPE = 4104
SO_SNDBUF = 4097
SO_RCVBUF = 4098
SO_SNDLOWAT = 4099
TCP_NODELAY = 1
MSG_OOB = 1
MSG_PEEK = 2
MSG_DONTROUTE = 4
IP_OPTIONS = 1
IP_DEFAULT_MULTICAST_LOOP = 1
IP_DEFAULT_MULTICAST_TTL = 1
IP_MULTICAST_IF = 2
IP_MULTICAST_TTL = 3
IP_MULTICAST_LOOP = 4
IP_ADD_MEMBERSHIP = 5
IP_DROP_MEMBERSHIP = 6
IP_TTL = 7
IP_TOS = 8
IP_MAX_MEMBERSHIPS = 20
EAI_ADDRFAMILY = 1
EAI_AGAIN = 2
EAI_BADFLAGS = 3
EAI_FAIL = 4
EAI_FAMILY = 5
EAI_MEMORY = 6
EAI_NODATA = 7
EAI_NONAME = 8
EAI_SERVICE = 9
EAI_SOCKTYPE = 10
EAI_SYSTEM = 11
EAI_BADHINTS = 12
EAI_PROTOCOL = 13
EAI_MAX = 14
AI_PASSIVE = 1
AI_CANONNAME = 2
AI_NUMERICHOST = 4
AI_MASK = 7
AI_ALL = 256
AI_V4MAPPED_CFG = 512
AI_ADDRCONFIG = 1024
AI_DEFAULT = 1536
AI_V4MAPPED = 2048
#--------------------------------------------------------------------------
# * Returns the associated IP address for the given hostname.
#--------------------------------------------------------------------------
def self.getaddress(host)
gethostbyname(host)[3].unpack("C4").join(".")
end
#--------------------------------------------------------------------------
# * Returns the associated IP address for the given hostname.
#--------------------------------------------------------------------------
def self.getservice(serv)
case serv
when Numeric
return serv
when String
return getservbyname(serv)
else
raise "Please use an integer or string for services."
end
end
#--------------------------------------------------------------------------
# * Returns information about the given hostname.
#--------------------------------------------------------------------------
def self.gethostbyname(name)
raise SocketError::ENOASSOCHOST if (ptr =
Winsock.gethostbyname(name)) == 0
host = ptr.copymem(16).unpack("iissi")
[host[0].copymem(64).split("\0")[0], [], host[2],
host[4].copymem(4).unpack("l")[0].copymem(4)]
end
#--------------------------------------------------------------------------
# * Returns the user's hostname.
#--------------------------------------------------------------------------
def self.gethostname
buf = "\0" * 256
Winsock.gethostname(buf, 256)
buf.strip
end
#--------------------------------------------------------------------------
# * Returns information about the given service.
#--------------------------------------------------------------------------
def self.getservbyname(name)
case name
when /echo/i
return 7
when /daytime/i
return 13
when /ftp/i
return 21
when /telnet/i
return 23
when /smtp/i
return 25
when /time/i
return 37
when /http/i
return 80
when /pop/i
return 110
else
#Network.testing? != 0 ? (Network.testresult(true)) : (raise
"Service not recognized.")
#return if Network.testing? == 2
end
end
#--------------------------------------------------------------------------
# * Creates an INET-sockaddr struct.
#--------------------------------------------------------------------------
def self.sockaddr_in(port, host)
begin
[AF_INET, getservice(port)].pack("sn") + gethostbyname(host)[3] +
[].pack("x8")
rescue
#Network.testing? != 0 ? (Network.testresult(true)): (nil)
#return if Network.testing? == 2
rescue Hangup
#Network.testing? != 0 ? (Network.testresult(true)): (nil)
#return if Network.testing? == 2
end
end
#--------------------------------------------------------------------------
# * Creates a new socket and connects it to the given host and port.
#--------------------------------------------------------------------------
def self.open(*args)
socket = new(*args)
if block_given?
begin
yield socket
ensure
socket.close
end
end
nil
end
#--------------------------------------------------------------------------
# * Creates a new socket.
#--------------------------------------------------------------------------
def initialize(domain, type, protocol)
SocketError.check if (@fd = Winsock.socket(domain, type, protocol))
== -1
@fd
end
#--------------------------------------------------------------------------
# * Accepts incoming connections.
#--------------------------------------------------------------------------
def accept(flags = 0)
buf = "\0" * 16
SocketError.check if Winsock.accept(@fd, buf, flags) == -1
buf
end
#--------------------------------------------------------------------------
# * Binds a socket to the given sockaddr.
#--------------------------------------------------------------------------
def bind(sockaddr)
SocketError.check if (ret = Winsock.bind(@fd, sockaddr,
sockaddr.size)) == -1
ret
end
#--------------------------------------------------------------------------
# * Closes a socket.
#--------------------------------------------------------------------------
def close
SocketError.check if (ret = Winsock.closesocket(@fd)) == -1
ret
end
#--------------------------------------------------------------------------
# * Connects a socket to the given sockaddr.
#--------------------------------------------------------------------------
def connect(sockaddr)
#return if Network.testing? == 2
SocketError.check if (ret = Winsock.connect(@fd, sockaddr,
sockaddr.size)) == -1
ret
end
#--------------------------------------------------------------------------
# * Listens for incoming connections.
#--------------------------------------------------------------------------
def listen(backlog)
SocketError.check if (ret = Winsock.listen(@fd, backlog)) == -1
ret
end
#--------------------------------------------------------------------------
# * Checks waiting data's status.
#--------------------------------------------------------------------------
def select(timeout) # timeout in seconds
SocketError.check if (ret = Winsock.select(1, [1, @fd].pack("ll"),
0, 0, [timeout.to_i,
(timeout * 1000000).to_i].pack("ll"))) == -1
ret
end
#--------------------------------------------------------------------------
# * Checks if data is waiting.
#--------------------------------------------------------------------------
def ready?
not select(0) == 0
end
#--------------------------------------------------------------------------
# * Reads data from socket.
#--------------------------------------------------------------------------
def read(len)
buf = "\0" * len
Win32API.new("msvcrt", "_read", "lpl", "l").call(@fd, buf, len)
buf
end
#--------------------------------------------------------------------------
# * Returns received data.
#--------------------------------------------------------------------------
def recv(len, flags = 0)
retString=""
remainLen=len
while remainLen > 0
buf = "\0" * remainLen
retval=Winsock.recv(@fd, buf, buf.size, flags)
SocketError.check if retval == -1
# Note: Return value may not equal requested length
remainLen-=retval
retString+=buf[0,retval]
end
return retString
end
#--------------------------------------------------------------------------
# * Sends data to a host.
#--------------------------------------------------------------------------
def send(data, flags = 0)
SocketError.check if (ret = Winsock.send(@fd, data, data.size,
flags)) == -1
ret
end
#--------------------------------------------------------------------------
# * Recieves file from a socket
# size : file size
# scene : update scene boolean
#--------------------------------------------------------------------------
def recv_file(size,scene=false,file="")
data = []
size.times do |i|
if scene == true
$scene.recv_update(size,i,file) if i%((size/1000)+1)== 0
else
Graphics.update if i%1024 == 0
end
data << recv(1)
end
return data
end
def recvTimeout
if select(10)==0
raise Hangup.new("Timeout")
end
return recv(1)
end
#--------------------------------------------------------------------------
# * Gets
#--------------------------------------------------------------------------
def gets
# Create buffer
message = ""
# Loop Until "end of line"
count=0
while true
x=select(0.05)
if x==0
count+=1
Graphics.update if count%10==0
raise Errno::ETIMEOUT if count>200
next
end
ch = recv(1)
break if ch == "\n"
message += ch
end
# Return recieved data
return message
end
#--------------------------------------------------------------------------
# * Writes data to socket.
#--------------------------------------------------------------------------
def write(data)
Win32API.new("msvcrt", "_write", "lpl", "l").call(@fd, data, 1)
end
end
#===============================================================================
# ** TCPSocket - Creates and manages TCP sockets.
#-------------------------------------------------------------------------------
# Author Ruby
# Version 1.8.1
#===============================================================================
#-------------------------------------------------------------------------------
# Begin SDK Enabled Check
#-------------------------------------------------------------------------------
class TCPSocket < Socket
#--------------------------------------------------------------------------
# * Creates a new socket and connects it to the given host and port.
#--------------------------------------------------------------------------
def self.open(*args)
socket = new(*args)
if block_given?
begin
yield socket
ensure
socket.close
end
end
nil
end
#--------------------------------------------------------------------------
# * Creates a new socket and connects it to the given host and port.
#--------------------------------------------------------------------------
def initialize(host, port)
super(AF_INET, SOCK_STREAM, IPPROTO_TCP)
connect(Socket.sockaddr_in(port, host))
end
end
#==============================================================================
# ** SocketError
#------------------------------------------------------------------------------
# Default exception class for sockets.
#==============================================================================
class SocketError < StandardError
ENOASSOCHOST = "getaddrinfo: no address associated with hostname."
def self.check
errno = Winsock.WSAGetLastError
#if not Network.testing? == 1
raise Errno.const_get(Errno.constants.detect { |c|
Errno.const_get(c).new.errno == errno })
#else
# errno != 0 ? (Network.testresult(true)) :
(Network.testresult(false))
#end
end
end
end # !Object.const_defined?(:Socket)
#############################
#
# HTTP utility functions
#
#############################
def pbPostData(url, postdata, filename=nil, depth=0)
userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"
if url[/^http:\/\/([^\/]+)(.*)$/]
host=$1
path=$2
path="/" if path.length==0
body = postdata.map {|key, value|
keyString=key.to_s
valueString=value.to_s
keyString.gsub!(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x',
s[0]) }
valueString.gsub!(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x',
s[0]) }
next "#{keyString}=#{valueString}"
}.join('&')
request="POST #{path} HTTP/1.1\nUser-Agent: #{userAgent}\nPragma:
no-cache\nHost: #{host}\nProxy-Connection: Close\n"
request+="Content-Type: application/x-www-form-urlencoded\n"
request+="Content-Length: #{body.length}\n"
request+="\n"
request+=body
return pbHttpRequest(host, request, filename, depth)
end
return ""
end
def pbDownloadData(url, filename=nil, depth=0)
userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US;
rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"
if depth>10
raise "Redirection level too deep"
end
if url[/^http:\/\/([^\/]+)(.*)$/]
host=$1
path=$2
path="/" if path.length==0
request="GET #{path} HTTP/1.1\nUser-Agent: #{userAgent}\nPragma:
no-cache\nHost: #{host}\nProxy-Connection: Close\n\n"
return pbHttpRequest(host, request, filename, depth)
end
return ""
end
def pbHttpRequest(host, request, filename=nil, depth=0)
if depth>10
raise "Redirection level too deep"
end
socket=::TCPSocket.new(host, 80)
time=Time.now.to_i
begin
socket.send(request)
result=socket.gets
data=""
# Get the HTTP result
if result[/^HTTP\/1\.[01] (\d+).*/]
errorcode=$1.to_i
if errorcode>=400 && errorcode<500
raise "HTTP Error #{errorcode}"
end
headers={}
# Get the response headers
while true
result=socket.gets.sub(/\r$/,"")
break if result==""
if result[/^([^:]+):\s*(.*)/]
headers[$1]=$2
end
end
length=-1
chunked=false
if headers["Content-Length"]
length=headers["Content-Length"].to_i
end
if headers["Transfer-Encoding"]=="chunked"
chunked=true
end
if headers["Location"] && errorcode >= 300 && errorcode < 400
socket.close rescue socket=nil
return pbDownloadData(headers["Location"],filename,depth+1)
end
if chunked==true
# Chunked content
while true
lengthline=socket.gets.sub(/\r$/,"")
length=lengthline.to_i(16)
break if length==0
while Time.now.to_i-time>=5 || socket.select(10)==0
time=Time.now.to_i
Graphics.update
end
data+=socket.recv(length)
socket.gets
end
elsif length==-1
# No content length specified
while true
if socket.select(500)==0
break
else
while Time.now.to_i-time>=5 || socket.select(10)==0
time=Time.now.to_i
Graphics.update
end
data+=socket.recv(1)
end
end
else
# Content length specified
while length>0
chunk=[length,4096].min
while Time.now.to_i-time>=5 || socket.select(10)==0
time=Time.now.to_i
Graphics.update
end
data+=socket.recv(chunk)
length-=chunk
end
end
end
if filename
File.open(filename,"wb"){|f|
f.write(data)
}
else
return data
end
ensure
socket.close rescue socket=nil
end
return ""
end
def pbDownloadToString(url)
begin
data=pbDownloadData(url)
return data
rescue
return ""
end
end
def pbDownloadToFile(url, file)
begin
pbDownloadData(url,file)
rescue
end
end
def pbPostToString(url, postdata)
begin
data=pbPostData(url, postdata)
return data
rescue
return ""
end
end
def pbPostToFile(url, postdata, file)
begin
pbPostData(url, postdata,file)
rescue
end
end
If I am wrong, the author uses something like the WinAPI integrated in
the RPG Maker or Windows, doesn't he?
Could it be, that i need to replace the code from Ruby 1.8.1 with the
one from 1.9.3?
--
Posted via http://www.ruby-forum.com/.