Synonym for IO.new
.
Returns self
.
Closes the stream for reading if open for reading; returns nil
. See Open and Closed Streams.
If the stream was opened by IO.popen
and is also closed for writing, sets global variable $?
(child exit status).
Example:
IO.popen('ruby', 'r+') do |pipe| puts pipe.closed? pipe.close_write puts pipe.closed? pipe.close_read puts $? puts pipe.closed? end
Output:
false false pid 14748 exit 0 true
Related: IO#close
, IO#close_write
, IO#closed?
.
Returns the path associated with the IO
, or nil
if there is no path associated with the IO
. It is not guaranteed that the path exists on the filesystem.
$stdin.path # => "<STDIN>" File.open("testfile") {|f| f.path} # => "testfile"
Reads at most maxlen bytes from ios using the read(2) system call after O_NONBLOCK is set for the underlying file descriptor.
If the optional outbuf argument is present, it must reference a String
, which will receive the data. The outbuf will contain only the received data after the method call even if it is not empty at the beginning.
read_nonblock
just calls the read(2) system call. It causes all errors the read(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The caller should care such errors.
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 read_nonblock.
read_nonblock
causes EOFError
on EOF.
On some platforms, such as Windows, non-blocking mode is not supported on IO
objects other than sockets. In such cases, Errno::EBADF will be raised.
If the read byte buffer is not empty, read_nonblock
reads from the buffer like readpartial. In this case, the read(2) system call is not called.
When read_nonblock
raises an exception kind of IO::WaitReadable
, read_nonblock
should not be called until io is readable for avoiding busy loop. This can be done as follows.
# emulates blocking read (readpartial). begin result = io.read_nonblock(maxlen) rescue IO::WaitReadable IO.select([io]) retry end
Although IO#read_nonblock
doesn’t raise IO::WaitWritable
. OpenSSL::Buffering#read_nonblock
can raise IO::WaitWritable
. If IO
and SSL should be used polymorphically, IO::WaitWritable
should be rescued too. See the document of OpenSSL::Buffering#read_nonblock
for sample code.
Note that this method is identical to readpartial except the non-blocking flag is set.
By specifying a keyword argument exception to false
, you can indicate that read_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable
instead. At EOF, it will return nil instead of raising EOFError
.
Returns a JSON
string representing self
:
require 'json/add/ostruct' puts OpenStruct.new('name' => 'Rowdy', :age => nil).to_json
Output:
{"json_class":"OpenStruct","t":{'name':'Rowdy',"age":null}}
Returns a JSON
string representing self
:
require 'json/add/range' puts (1..4).to_json puts (1...4).to_json puts ('a'..'d').to_json
Output:
{"json_class":"Range","a":[1,4,false]} {"json_class":"Range","a":[1,4,true]} {"json_class":"Range","a":["a","d",false]}
With a block given, passes each element of self
to the block in reverse order:
a = [] (1..4).reverse_each {|element| a.push(element) } # => 1..4 a # => [4, 3, 2, 1] a = [] (1...4).reverse_each {|element| a.push(element) } # => 1...4 a # => [3, 2, 1]
With no block given, returns an enumerator.
Returns a JSON
string representing self
:
require 'json/add/regexp' puts /foo/.to_json
Output:
{"json_class":"Regexp","o":0,"s":"foo"}
With no argument, returns the value of $!
, which is the result of the most recent pattern match (see Regexp global variables):
/c(.)t/ =~ 'cat' # => 0 Regexp.last_match # => #<MatchData "cat" 1:"a"> /a/ =~ 'foo' # => nil Regexp.last_match # => nil
With non-negative integer argument n
, returns the _n_th field in the matchdata, if any, or nil if none:
/c(.)t/ =~ 'cat' # => 0 Regexp.last_match(0) # => "cat" Regexp.last_match(1) # => "a" Regexp.last_match(2) # => nil
With negative integer argument n
, counts backwards from the last field:
Regexp.last_match(-1) # => "a"
With string or symbol argument name
, returns the string value for the named capture, if any:
/(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ 'var = val' Regexp.last_match # => #<MatchData "var = val" lhs:"var"rhs:"val"> Regexp.last_match(:lhs) # => "var" Regexp.last_match('rhs') # => "val" Regexp.last_match('foo') # Raises IndexError.
Returns a hash representing named captures of self
(see Named Captures):
Each key is the name of a named capture.
Each value is an array of integer indexes for that named capture.
Examples:
/(?<foo>.)(?<bar>.)/.named_captures # => {"foo"=>[1], "bar"=>[2]} /(?<foo>.)(?<foo>.)/.named_captures # => {"foo"=>[1, 2]} /(.)(.)/.named_captures # => {}
See as_json
.
Returns a JSON
string representing self
:
require 'json/add/set' puts Set.new(%w/foo bar baz/).to_json
Output:
{"json_class":"Set","a":["foo","bar","baz"]}
Returns self if no arguments are given. Otherwise, converts the set to another with klass.new(self, *args, &block)
.
In subclasses, returns klass.new(self, *args, &block)
unless overridden.
Returns a JSON
string representing self
:
require 'json/add/struct' Customer = Struct.new('Customer', :name, :address, :zip) puts Struct::Customer.new.to_json
Output:
{"json_class":"Struct","t":{'name':'Rowdy',"age":null}}
Returns true
if the class was initialized with keyword_init: true
. Otherwise returns nil
or false
.
Examples:
Foo = Struct.new(:a) Foo.keyword_init? # => nil Bar = Struct.new(:a, keyword_init: true) Bar.keyword_init? # => true Baz = Struct.new(:a, keyword_init: false) Baz.keyword_init? # => false
Returns a hash of the name/value pairs for the given member names.
Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) h = joe.deconstruct_keys([:zip, :address]) h # => {:zip=>12345, :address=>"123 Maple, Anytown NC"}
Returns all names and values if array_of_names
is nil
:
h = joe.deconstruct_keys(nil) h # => {:name=>"Joseph Smith, Jr.", :address=>"123 Maple, Anytown NC", :zip=>12345}
Returns a JSON
string representing self
:
require 'json/add/symbol' puts :foo.to_json
Output:
# {"json_class":"Symbol","s":"foo"}
Returns a Proc
object which calls the method with name of self
on the first parameter and passes the remaining parameters to the method.
proc = :to_s.to_proc # => #<Proc:0x000001afe0e48680(&:to_s) (lambda)> proc.call(1000) # => "1000" proc.call(1000, 16) # => "3e8" (1..3).collect(&:to_s) # => ["1", "2", "3"]
Equivalent to self.to_s.start_with?
; see String#start_with?
.