returns the socket address as packed struct sockaddr string.
Addrinfo.tcp("localhost", 80).to_sockaddr #=> "\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
Receives up to maxlen bytes from udpsocket
using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. flags is zero or more of the MSG_
options. The first element of the results, mesg, is the data received. The second element, sender_inet_addr, is an array to represent the sender address.
When recvfrom(2) returns 0, Socket#recv_nonblock
returns nil. In most cases it means the connection was closed, but it may also mean an empty packet was received, as the underlying API makes it impossible to distinguish these two cases.
maxlen
- the number of bytes to receive from the socket
flags
- zero or more of the MSG_
options
outbuf
- destination String
buffer
options
- keyword hash, supporting ‘exception: false`
require 'socket' s1 = UDPSocket.new s1.bind("127.0.0.1", 0) s2 = UDPSocket.new s2.bind("127.0.0.1", 0) s2.connect(*s1.addr.values_at(3,1)) s1.connect(*s2.addr.values_at(3,1)) s1.send "aaa", 0 begin # emulate blocking recvfrom p s2.recvfrom_nonblock(10) #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]] rescue IO::WaitReadable IO.select([s2]) retry end
Refer to Socket#recvfrom
for the exceptions that may be thrown if the call to recvfrom_nonblock fails.
UDPSocket#recvfrom_nonblock
may raise any error corresponding to recvfrom(2) failure, including Errno::EWOULDBLOCK.
If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying recvfrom_nonblock.
By specifying a keyword argument exception to false
, you can indicate that recvfrom_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable
instead.
Example
UNIXServer.open("/tmp/sock") {|serv| UNIXSocket.open("/tmp/sock") {|c| s = serv.accept c.send_io STDOUT stdout = s.recv_io p STDOUT.fileno #=> 1 p stdout.fileno #=> 7 stdout.puts "hello" # outputs "hello\n" to standard output. } }
klass will determine the class of io returned (using the IO.for_fd
singleton method or similar). If klass is nil
, an integer file descriptor is returned.
mode is the same as the argument passed to IO.for_fd
Closes self
for reading; closed-write setting remains unchanged.
Raises IOError
if reading is attempted.
Related: StringIO#close
, StringIO#close_write
.
Returns true
if self
is closed for reading, false
otherwise.
Returns the substring that precedes the matched substring from the most recent match attempt if it was successful, or nil
otherwise; see [Basic Match Values]:
scanner = StringScanner.new('foobarbaz') scanner.pre_match # => nil scanner.pos = 3 scanner.exist?(/baz/) # => 6 scanner.pre_match # => "foobar" # Substring of entire string, not just target string. scanner.exist?(/nope/) # => nil scanner.pre_match # => nil
Returns the substring that follows the matched substring from the most recent match attempt if it was successful, or nil
otherwise; see [Basic Match Values]:
scanner = StringScanner.new('foobarbaz') scanner.post_match # => nil scanner.pos = 3 scanner.match?(/bar/) # => 3 scanner.post_match # => "baz" scanner.match?(/nope/) # => nil scanner.post_match # => nil
Returns whether the [fixed-anchor property] is set.
Returns the array of captured match values at indexes (1..) if the most recent match attempt succeeded, or nil otherwise; see [Captured Match Values]:
scanner = StringScanner.new('Fri Dec 12 1975 14:39') scanner.named_captures # => {} pattern = /(?<wday>\w+) (?<month>\w+) (?<day>\d+) / scanner.match?(pattern) scanner.named_captures # => {"wday"=>"Fri", "month"=>"Dec", "day"=>"12"} scanner.string = 'nope' scanner.match?(pattern) scanner.named_captures # => {"wday"=>nil, "month"=>nil, "day"=>nil} scanner.match?(/nosuch/) scanner.named_captures # => {}
Evaluates a string containing Ruby
source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self
is set to obj while the code is executing, giving the code access to obj’s instance variables and private methods.
When instance_eval
is given a block, obj is also passed in as the block’s only argument.
When instance_eval
is given a String
, the optional second and third parameters supply a filename and starting line number that are used when reporting compilation errors.
class KlassWithSecret def initialize @secret = 99 end private def the_secret "Ssssh! The secret is #{@secret}." end end k = KlassWithSecret.new k.instance_eval { @secret } #=> 99 k.instance_eval { the_secret } #=> "Ssssh! The secret is 99." k.instance_eval {|obj| obj == self } #=> true
Executes the given block within the context of the receiver (obj). In order to set the context, the variable self
is set to obj while the code is executing, giving the code access to obj’s instance variables. Arguments are passed as block parameters.
class KlassWithSecret def initialize @secret = 99 end end k = KlassWithSecret.new k.instance_exec(5) {|x| @secret+x } #=> 104
Returns a Proc
object that maps a key to its value:
h = {foo: 0, bar: 1, baz: 2} proc = h.to_proc proc.class # => Proc proc.call(:foo) # => 0 proc.call(:bar) # => 1 proc.call(:nosuch) # => nil
Related: see Methods for Converting.
With an argument, a block, or both given, derives a new hash new_hash
from self
, the argument, and/or the block; all, some, or none of its keys may be different from those in self
.
With a block given and no argument, new_hash
has keys determined only by the block.
For each key/value pair old_key/value
in self
, calls the block with old_key
; the block’s return value becomes new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys {|old_key| old_key.to_s } # => {"foo" => 0, "bar" => 1, "baz" => 2} h.transform_keys {|old_key| 'xxx' } # => {"xxx" => 2}
With argument other_hash
given and no block, new_hash
may have new keys provided by other_hash
and unchanged keys provided by self
.
For each key/value pair old_key/old_value
in self
, looks for key old_key
in other_hash
:
If old_key
is found, its value other_hash[old_key]
is taken as new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) # => {FOO: 0, BAR: 1, BAZ: 2} h.transform_keys(baz: :FOO, bar: :FOO, foo: :FOO) # => {FOO: 2}
If old_key
is not found, sets new_hash[old_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys({}) # => {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :foo) # => {foo: 2, bar: 1}
Unused keys in other_hash
are ignored:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(bat: 3) # => {foo: 0, bar: 1, baz: 2}
With both argument other_hash
and a block given, new_hash
has new keys specified by other_hash
or by the block, and unchanged keys provided by self
.
For each pair old_key
and value
in self
:
If other_hash
has key old_key
(with value new_key
), does not call the block for that key; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' } # => {FOO: 0, BAR: 1, BAZ: 2}
If other_hash
does not have key old_key
, calls the block with old_key
and takes its return value as new_key
; sets new_hash[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys(baz: :BAZ) {|key| key.to_s.reverse } # => {"oof" => 0, "rab" => 1, BAZ: 2} h.transform_keys(baz: :BAZ) {|key| 'ook' } # => {"ook" => 1, BAZ: 2}
With no argument and no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With an argument, a block, or both given, derives keys from the argument, the block, and self
; all, some, or none of the keys in self
may be changed.
With a block given and no argument, derives keys only from the block; all, some, or none of the keys in self
may be changed.
For each key/value pair old_key/value
in self
, calls the block with old_key
; the block’s return value becomes new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys! {|old_key| old_key.to_s } # => {"foo" => 0, "bar" => 1, "baz" => 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys! {|old_key| 'xxx' } # => {"xxx" => 2}
With argument other_hash
given and no block, derives keys for self
from other_hash
and self
; all, some, or none of the keys in self
may be changed.
For each key/value pair old_key/old_value
in self
, looks for key old_key
in other_hash
:
If old_key
is found, takes value other_hash[old_key]
as new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) # => {FOO: 0, BAR: 1, BAZ: 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :FOO, bar: :FOO, foo: :FOO) # => {FOO: 2}
If old_key
is not found, does nothing:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!({}) # => {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :foo) # => {foo: 2, bar: 1}
Unused keys in other_hash
are ignored:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(bat: 3) # => {foo: 0, bar: 1, baz: 2}
With both argument other_hash
and a block given, derives keys from other_hash
, the block, and self
; all, some, or none of the keys in self
may be changed.
For each pair old_key
and value
in self
:
If other_hash
has key old_key
(with value new_key
), does not call the block for that key; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' } # => {FOO: 0, BAR: 1, BAZ: 2}
If other_hash
does not have key old_key
, calls the block with old_key
and takes its return value as new_key
; removes the entry for old_key
: self.delete(old_key)
; sets self[new_key] = value
; a duplicate key overwrites:
h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ) {|key| key.to_s.reverse } # => {"oof" => 0, "rab" => 1, BAZ: 2} h = {foo: 0, bar: 1, baz: 2} h.transform_keys!(baz: :BAZ) {|key| 'ook' } # => {"ook" => 1, BAZ: 2}
With no argument and no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With a block given, returns a new hash new_hash
; for each pair key
/value
in self
, calls the block with value
and captures its return as new_value
; adds to new_hash
the entry key
/new_value
:
h = {foo: 0, bar: 1, baz: 2} h1 = h.transform_values {|value| value * 100} h1 # => {foo: 0, bar: 100, baz: 200}
With no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
With a block given, changes the values of self
as determined by the block; returns self
.
For each entry key
/old_value
in self
, calls the block with old_value
, captures its return value as new_value
, and sets self[key] = new_value
:
h = {foo: 0, bar: 1, baz: 2} h.transform_values! {|value| value * 100} # => {foo: 0, bar: 100, baz: 200}
With no block given, returns a new Enumerator
.
Related: see Methods for Transforming Keys and Values.
Returns a Hash
containing all name/value pairs from ENV:
ENV.replace('foo' => '0', 'bar' => '1') ENV.to_hash # => {"bar"=>"1", "foo"=>"0"}
Returns an IO
object representing the current file. This will be a File
object unless the current file is a stream such as STDIN.
For example:
ARGF.to_io #=> #<File:glark.txt> ARGF.to_io #=> #<IO:<STDIN>>
Reads at most maxlen bytes from the ARGF
stream in non-blocking mode.
Returns a new binding each time near TOPLEVEL_BINDING for runs that do not specify a binding.
Creates a new ipaddr containing the given network byte ordered string form of an IP address.
Returns a json string containing the IP address representation.
Creates a Range
object for the network address.
Returns the usable width for out
. As the width of out
:
If out
is assigned to a tty device, its width is used.
Otherwise, or it could not get the value, the COLUMN
environment variable is assumed to be set to the width.
If COLUMN
is not set to a non-zero number, 80 is assumed.
And finally, returns the above width value - 1.
This -1 is for Windows command prompt, which moves the cursor to the next line if it reaches the last column.