Returns the member names from self
as an array:
Customer = Struct.new(:name, :address, :zip) Customer.new.members # => [:name, :address, :zip]
Related: to_a
.
Equivalent to self.to_s.length
; see String#length
.
Equivalent to self.to_s.match
, including possible updates to global variables; see String#match
.
Equivalent to sym.to_s.match?
; see String#match
.
Allocates space for a new object of class’s class and does not call initialize on the new instance. The returned object must be an instance of class.
klass = Class.new do def initialize(*args) @initialized = true end def initialized? @initialized || false end end klass.allocate.initialized? #=> false
Spawns the specified command on a newly allocated pty. You can also use the alias ::getpty
.
The command’s controlling tty is set to the slave device of the pty and its standard input/output/error is redirected to the slave device.
env
is an optional hash that provides additional environment variables to the spawned pty.
# sets FOO to "bar" PTY.spawn({"FOO"=>"bar"}, "printenv", "FOO") do |r, w, pid| p r.read #=> "bar\r\n" ensure r.close; w.close; Process.wait(pid) end # unsets FOO PTY.spawn({"FOO"=>nil}, "printenv", "FOO") do |r, w, pid| p r.read #=> "" ensure r.close; w.close; Process.wait(pid) end
command
and command_line
are the full commands to run, given a String
. Any additional arguments
will be passed to the command.
In the non-block form this returns an array of size three, [r, w, pid]
.
In the block form these same values will be yielded to the block:
r
A readable IO
that contains the command’s standard output and standard error
w
A writable IO
that is the command’s standard input
pid
The process identifier for the command.
This method does not clean up like closing IOs or waiting for child process, except that the process is detached in the block form to prevent it from becoming a zombie (see Process.detach
). Any other cleanup is the responsibility of the caller. If waiting for pid
, be sure to close both r
and w
before doing so; doing it in the reverse order may cause deadlock on some OSes.
Parses the given Ruby
program read from src
. src
must be a String
or an IO
or a object with a gets
method.
Creates a pair of sockets connected each other.
domain should be a communications domain such as: :INET, :INET6, :UNIX, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain, defaults to 0 for the domain.
s1, s2 = Socket.pair(:UNIX, :STREAM, 0) s1.send "a", 0 s1.send "b", 0 s1.close p s2.recv(10) #=> "ab" p s2.recv(10) #=> "" p s2.recv(10) #=> "" s1, s2 = Socket.pair(:UNIX, :DGRAM, 0) s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "a" p s2.recv(10) #=> "b"
Creates a pair of sockets connected each other.
domain should be a communications domain such as: :INET, :INET6, :UNIX, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain, defaults to 0 for the domain.
s1, s2 = Socket.pair(:UNIX, :STREAM, 0) s1.send "a", 0 s1.send "b", 0 s1.close p s2.recv(10) #=> "ab" p s2.recv(10) #=> "" p s2.recv(10) #=> "" s1, s2 = Socket.pair(:UNIX, :DGRAM, 0) s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "a" p s2.recv(10) #=> "b"
Use Addrinfo#getnameinfo
instead. This method is deprecated for the following reasons:
Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.
gethostbyaddr() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method obtains the host information for address.
p Socket.gethostbyaddr([221,186,184,68].pack("CCCC")) #=> ["carbon.ruby-lang.org", [], 2, "\xDD\xBA\xB8D"] p Socket.gethostbyaddr([127,0,0,1].pack("CCCC")) ["localhost", [], 2, "\x7F\x00\x00\x01"] p Socket.gethostbyaddr(([0]*15+[1]).pack("C"*16)) #=> ["localhost", ["ip6-localhost", "ip6-loopback"], 10, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"]
returns the address family as an integer.
Addrinfo.tcp("localhost", 80).afamily == Socket::AF_INET #=> true
returns the protocol family as an integer.
Addrinfo.tcp("localhost", 80).pfamily == Socket::PF_INET #=> true
Creates a pair of sockets connected to each other.
type should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain. 0 is default protocol for the domain.
s1, s2 = UNIXSocket.pair s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "ab"
Creates a pair of sockets connected to each other.
type should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain. 0 is default protocol for the domain.
s1, s2 = UNIXSocket.pair s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "ab"
Returns false
. Just for compatibility to IO
.
Returns the size of the buffer string.
Truncates the buffer string to at most integer bytes. The stream must be opened for writing.
Appends the given more_string
to the [stored string].
Returns self
.
Does not affect the [positions] or [match values].
scanner = StringScanner.new('foo') scanner.string # => "foo" scanner.terminate scanner.concat('barbaz') # => #<StringScanner 3/9 "foo" @ "barba..."> scanner.string # => "foobarbaz" put_situation(scanner) # Situation: # pos: 3 # charpos: 3 # rest: "barbaz" # rest_size: 6
Attempts to [match] the given pattern
at the beginning of the [target substring]; does not modify the [positions].
If the match succeeds:
Sets [match values].
Returns the size in bytes of the matched substring.
scanner = StringScanner.new('foobarbaz') scanner.pos = 3 scanner.match?(/bar/) => 3 put_match_values(scanner) # Basic match values: # matched?: true # matched_size: 3 # pre_match: "foo" # matched : "bar" # post_match: "baz" # Captured match values: # size: 1 # captures: [] # named_captures: {} # values_at: ["bar", nil] # []: # [0]: "bar" # [1]: nil put_situation(scanner) # Situation: # pos: 3 # charpos: 3 # rest: "barbaz" # rest_size: 6
If the match fails:
Clears match values.
Returns nil
.
Does not increment positions.
scanner.match?(/nope/) # => nil match_values_cleared?(scanner) # => true
Returns true
of the most recent [match attempt] was successful, false
otherwise; see [Basic Matched Values]:
scanner = StringScanner.new('foobarbaz') scanner.matched? # => false scanner.pos = 3 scanner.exist?(/baz/) # => 6 scanner.matched? # => true scanner.exist?(/nope/) # => nil scanner.matched? # => false
Returns the matched substring from the most recent [match] attempt if it was successful, or nil
otherwise; see [Basic Matched Values]:
scanner = StringScanner.new('foobarbaz') scanner.matched # => nil scanner.pos = 3 scanner.match?(/bar/) # => 3 scanner.matched # => "bar" scanner.match?(/nope/) # => nil scanner.matched # => nil
Merges each of other_hashes
into self
; returns self
.
Each argument in other_hashes
must be a Hash
.
With arguments and no block:
Returns self
, after the given hashes are merged into it.
The given hashes are merged left to right.
Each new entry is added at the end.
Each duplicate-key entry’s value overwrites the previous value.
Example:
h = {foo: 0, bar: 1, baz: 2} h1 = {bat: 3, bar: 4} h2 = {bam: 5, bat:6} h.merge!(h1, h2) # => {foo: 0, bar: 4, baz: 2, bat: 6, bam: 5}
With arguments and a block:
Returns self
, after the given hashes are merged.
The given hashes are merged left to right.
Each new-key entry is added at the end.
For each duplicate key:
Calls the block with the key and the old and new values.
The block’s return value becomes the new value for the entry.
Example:
h = {foo: 0, bar: 1, baz: 2} h1 = {bat: 3, bar: 4} h2 = {bam: 5, bat:6} h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value } h3 # => {foo: 0, bar: 5, baz: 2, bat: 9, bam: 5}
With no arguments:
Returns self
, unmodified.
The block, if given, is ignored.
Example:
h = {foo: 0, bar: 1, baz: 2} h.merge # => {foo: 0, bar: 1, baz: 2} h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' } h1 # => {foo: 0, bar: 1, baz: 2}
Merges each of other_hashes
into self
; returns self
.
Each argument in other_hashes
must be a Hash
.
With arguments and no block:
Returns self
, after the given hashes are merged into it.
The given hashes are merged left to right.
Each new entry is added at the end.
Each duplicate-key entry’s value overwrites the previous value.
Example:
h = {foo: 0, bar: 1, baz: 2} h1 = {bat: 3, bar: 4} h2 = {bam: 5, bat:6} h.merge!(h1, h2) # => {foo: 0, bar: 4, baz: 2, bat: 6, bam: 5}
With arguments and a block:
Returns self
, after the given hashes are merged.
The given hashes are merged left to right.
Each new-key entry is added at the end.
For each duplicate key:
Calls the block with the key and the old and new values.
The block’s return value becomes the new value for the entry.
Example:
h = {foo: 0, bar: 1, baz: 2} h1 = {bat: 3, bar: 4} h2 = {bam: 5, bat:6} h3 = h.merge!(h1, h2) { |key, old_value, new_value| old_value + new_value } h3 # => {foo: 0, bar: 5, baz: 2, bat: 9, bam: 5}
With no arguments:
Returns self
, unmodified.
The block, if given, is ignored.
Example:
h = {foo: 0, bar: 1, baz: 2} h.merge # => {foo: 0, bar: 1, baz: 2} h1 = h.merge! { |key, old_value, new_value| raise 'Cannot happen' } h1 # => {foo: 0, bar: 1, baz: 2}
Returns the new Hash
formed by merging each of other_hashes
into a copy of self
.
Each argument in other_hashes
must be a Hash
.
With arguments and no block:
Returns the new Hash
object formed by merging each successive Hash
in other_hashes
into self
.
Each new-key entry is added at the end.
Each duplicate-key entry’s value overwrites the previous value.
Example:
h = {foo: 0, bar: 1, baz: 2} h1 = {bat: 3, bar: 4} h2 = {bam: 5, bat:6} h.merge(h1, h2) # => {foo: 0, bar: 4, baz: 2, bat: 6, bam: 5}
With arguments and a block:
Returns a new Hash
object that is the merge of self
and each given hash.
The given hashes are merged left to right.
Each new-key entry is added at the end.
For each duplicate key:
Calls the block with the key and the old and new values.
The block’s return value becomes the new value for the entry.
Example:
h = {foo: 0, bar: 1, baz: 2} h1 = {bat: 3, bar: 4} h2 = {bam: 5, bat:6} h3 = h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value } h3 # => {foo: 0, bar: 5, baz: 2, bat: 9, bam: 5}
With no arguments:
Returns a copy of self
.
The block, if given, is ignored.
Example:
h = {foo: 0, bar: 1, baz: 2} h.merge # => {foo: 0, bar: 1, baz: 2} h1 = h.merge { |key, old_value, new_value| raise 'Cannot happen' } h1 # => {foo: 0, bar: 1, baz: 2}