Equivalent to self.to_s.match
, including possible updates to global variables; see String#match
.
Equivalent to sym.to_s.match?
; see String#match
.
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
Returns the count of entries in self
:
{foo: 0, bar: 1, baz: 2}.size # => 3
Related: see Methods for Querying.
Updates values and/or adds entries to self
; returns self
.
Each argument other_hash
in other_hashes
must be a hash.
With no block given, for each successive entry key
/new_value
in each successive other_hash
:
If key
is in self
, sets self[key] = new_value
, whose position is unchanged:
h0 = {foo: 0, bar: 1, baz: 2} h1 = {bar: 3, foo: -1} h0.update(h1) # => {foo: -1, bar: 3, baz: 2}
If key
is not in self
, adds the entry at the end of self
:
h = {foo: 0, bar: 1, baz: 2} h.update({bam: 3, bah: 4}) # => {foo: 0, bar: 1, baz: 2, bam: 3, bah: 4}
With a block given, for each successive entry key
/new_value
in each successive other_hash
:
If key
is in self
, fetches old_value
from self[key]
, calls the block with key
, old_value
, and new_value
, and sets self[key] = new_value
, whose position is unchanged :
season = {AB: 75, H: 20, HR: 3, SO: 17, W: 11, HBP: 3} today = {AB: 3, H: 1, W: 1} yesterday = {AB: 4, H: 2, HR: 1} season.update(yesterday, today) {|key, old_value, new_value| old_value + new_value } # => {AB: 82, H: 23, HR: 4, SO: 17, W: 12, HBP: 3}
If key
is not in self
, adds the entry at the end of self
:
h = {foo: 0, bar: 1, baz: 2} h.update({bat: 3}) { fail 'Cannot happen' } # => {foo: 0, bar: 1, baz: 2, bat: 3}
Related: see Methods for Assigning.
Updates values and/or adds entries to self
; returns self
.
Each argument other_hash
in other_hashes
must be a hash.
With no block given, for each successive entry key
/new_value
in each successive other_hash
:
If key
is in self
, sets self[key] = new_value
, whose position is unchanged:
h0 = {foo: 0, bar: 1, baz: 2} h1 = {bar: 3, foo: -1} h0.update(h1) # => {foo: -1, bar: 3, baz: 2}
If key
is not in self
, adds the entry at the end of self
:
h = {foo: 0, bar: 1, baz: 2} h.update({bam: 3, bah: 4}) # => {foo: 0, bar: 1, baz: 2, bam: 3, bah: 4}
With a block given, for each successive entry key
/new_value
in each successive other_hash
:
If key
is in self
, fetches old_value
from self[key]
, calls the block with key
, old_value
, and new_value
, and sets self[key] = new_value
, whose position is unchanged :
season = {AB: 75, H: 20, HR: 3, SO: 17, W: 11, HBP: 3} today = {AB: 3, H: 1, W: 1} yesterday = {AB: 4, H: 2, HR: 1} season.update(yesterday, today) {|key, old_value, new_value| old_value + new_value } # => {AB: 82, H: 23, HR: 4, SO: 17, W: 12, HBP: 3}
If key
is not in self
, adds the entry at the end of self
:
h = {foo: 0, bar: 1, baz: 2} h.update({bat: 3}) { fail 'Cannot happen' } # => {foo: 0, bar: 1, baz: 2, bat: 3}
Related: see Methods for Assigning.
Each argument other_hash
in other_hashes
must be a hash.
With arguments other_hashes
given and no block, returns the new hash formed by merging each successive other_hash
into a copy of self
; returns that copy; for each successive entry in other_hash
:
For a new key, the entry is added at the end of self
.
For duplicate key, the entry overwrites the entry in self
, whose position is unchanged.
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 other_hashes
and a block given, behaves as above except that for a duplicate key the overwriting entry takes it value not from the entry in other_hash
, but instead from the block:
The block is called with the duplicate key and the values from both self
and other_hash
.
The block’s return value becomes the new value for the entry in self
.
Example:
h = {foo: 0, bar: 1, baz: 2} h1 = {bat: 3, bar: 4} h2 = {bam: 5, bat:6} h.merge(h1, h2) { |key, old_value, new_value| old_value + new_value } # => {foo: 0, bar: 5, baz: 2, bat: 9, bam: 5}
With no arguments, returns a copy of self
; the block, if given, is ignored.
Related: see Methods for Assigning.
With positive integer depth
, returns a new array that is a recursive flattening of self
to the given depth
.
At each level of recursion:
Each element whose value is an array is “flattened” (that is, replaced by its individual array elements); see Array#flatten
.
Each element whose value is not an array is unchanged. even if the value is an object that has instance method flatten (such as a hash).
Examples; note that entry foo: {bar: 1, baz: 2}
is never flattened.
h = {foo: {bar: 1, baz: 2}, bat: [:bam, [:bap, [:bah]]]} h.flatten(1) # => [:foo, {:bar=>1, :baz=>2}, :bat, [:bam, [:bap, [:bah]]]] h.flatten(2) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, [:bap, [:bah]]] h.flatten(3) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, [:bah]] h.flatten(4) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah] h.flatten(5) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah]
With negative integer depth
, flattens all levels:
h.flatten(-1) # => [:foo, {:bar=>1, :baz=>2}, :bat, :bam, :bap, :bah]
With depth
zero, returns the equivalent of to_a
:
h.flatten(0) # => [[:foo, {:bar=>1, :baz=>2}], [:bat, [:bam, [:bap, [:bah]]]]]
Related: see Methods for Converting.
Returns a copy of self
with all nil
-valued entries removed:
h = {foo: 0, bar: nil, baz: 2, bat: nil} h.compact # => {foo: 0, baz: 2}
Related: see Methods for Deleting.
If self
contains any nil
-valued entries, returns self
with all nil
-valued entries removed; returns nil
otherwise:
h = {foo: 0, bar: nil, baz: 2, bat: nil} h.compact! h # => {foo: 0, baz: 2} h.compact! # => nil
Related: see Methods for Deleting.