Results for: "module_function"

Return the number of seconds the specified time zone differs from UTC.

Numeric time zones that include minutes, such as -10:00 or +1330 will work, as will simpler hour-only time zones like -10 or +13.

Textual time zones listed in ZoneOffset are also supported.

If the time zone does not match any of the above, zone_offset will check if the local time zone (both with and without potential Daylight Saving Time changes being in effect) matches zone. Specifying a value for year will change the year used to find the local time zone.

If zone_offset is unable to determine the offset, nil will be returned.

require 'time'

Time.zone_offset("EST") #=> -18000

You must require ‘time’ to use this method.

Waits until IO is priority and returns a truthy value or a falsy value when times out. Priority data is sent and received using the Socket::MSG_OOB flag and is typically limited to streams.

You must require ‘io/wait’ to use this method.

Attempts to convert object into an IO object via method to_io; returns the new IO object if successful, or nil otherwise:

IO.try_convert(STDOUT)   # => #<IO:<STDOUT>>
IO.try_convert(ARGF)     # => #<IO:<STDIN>>
IO.try_convert('STDOUT') # => nil

Returns self.

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.

Writes the given string to ios using the write(2) system call after O_NONBLOCK is set for the underlying file descriptor.

It returns the number of bytes written.

write_nonblock just calls the write(2) system call. It causes all errors the write(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. The result may also be smaller than string.length (partial write). The caller should care such errors and partial write.

If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitWritable. So IO::WaitWritable can be used to rescue the exceptions for retrying write_nonblock.

# Creates a pipe.
r, w = IO.pipe

# write_nonblock writes only 65536 bytes and return 65536.
# (The pipe size is 65536 bytes on this environment.)
s = "a" * 100000
p w.write_nonblock(s)     #=> 65536

# write_nonblock cannot write a byte and raise EWOULDBLOCK (EAGAIN).
p w.write_nonblock("b")   # Resource temporarily unavailable (Errno::EAGAIN)

If the write buffer is not empty, it is flushed at first.

When write_nonblock raises an exception kind of IO::WaitWritable, write_nonblock should not be called until io is writable for avoiding busy loop. This can be done as follows.

begin
  result = io.write_nonblock(string)
rescue IO::WaitWritable, Errno::EINTR
  IO.select(nil, [io])
  retry
end

Note that this doesn’t guarantee to write all data in string. The length written is reported as result and it should be checked later.

On some platforms such as Windows, write_nonblock is not supported according to the kind of the IO object. In such cases, write_nonblock raises Errno::EBADF.

By specifying a keyword argument exception to false, you can indicate that write_nonblock should not raise an IO::WaitWritable exception, but return the symbol :wait_writable instead.

See as_json.

Methods BigDecimal#as_json and BigDecimal.json_create may be used to serialize and deserialize a BigDecimal object; see Marshal.

Method BigDecimal#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/bigdecimal'
x = BigDecimal(2).as_json             # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}
y = BigDecimal(2.0, 4).as_json        # => {"json_class"=>"BigDecimal", "b"=>"36:0.2e1"}
z = BigDecimal(Complex(2, 0)).as_json # => {"json_class"=>"BigDecimal", "b"=>"27:0.2e1"}

Method JSON.create deserializes such a hash, returning a BigDecimal object:

BigDecimal.json_create(x) # => 0.2e1
BigDecimal.json_create(y) # => 0.2e1
BigDecimal.json_create(z) # => 0.2e1

Returns a JSON string representing self:

require 'json/add/bigdecimal'
puts BigDecimal(2).to_json
puts BigDecimal(2.0, 4).to_json
puts BigDecimal(Complex(2, 0)).to_json

Output:

{"json_class":"BigDecimal","b":"27:0.2e1"}
{"json_class":"BigDecimal","b":"36:0.2e1"}
{"json_class":"BigDecimal","b":"27:0.2e1"}

See as_json.

Methods OpenStruct#as_json and OpenStruct.json_create may be used to serialize and deserialize a OpenStruct object; see Marshal.

Method OpenStruct#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/ostruct'
x = OpenStruct.new('name' => 'Rowdy', :age => nil).as_json
# => {"json_class"=>"OpenStruct", "t"=>{:name=>'Rowdy', :age=>nil}}

Method JSON.create deserializes such a hash, returning a OpenStruct object:

OpenStruct.json_create(x)
# => #<OpenStruct name='Rowdy', age=nil>

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}}

See as_json.

Methods Range#as_json and Range.json_create may be used to serialize and deserialize a Range object; see Marshal.

Method Range#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/range'
x = (1..4).as_json     # => {"json_class"=>"Range", "a"=>[1, 4, false]}
y = (1...4).as_json    # => {"json_class"=>"Range", "a"=>[1, 4, true]}
z = ('a'..'d').as_json # => {"json_class"=>"Range", "a"=>["a", "d", false]}

Method JSON.create deserializes such a hash, returning a Range object:

Range.json_create(x) # => 1..4
Range.json_create(y) # => 1...4
Range.json_create(z) # => "a".."d"

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]}

See as_json.

Methods Rational#as_json and Rational.json_create may be used to serialize and deserialize a Rational object; see Marshal.

Method Rational#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/rational'
x = Rational(2, 3).as_json
# => {"json_class"=>"Rational", "n"=>2, "d"=>3}

Method JSON.create deserializes such a hash, returning a Rational object:

Rational.json_create(x)
# => (2/3)

Returns a JSON string representing self:

require 'json/add/rational'
puts Rational(2, 3).to_json

Output:

{"json_class":"Rational","n":2,"d":3}

See as_json.

Methods Regexp#as_json and Regexp.json_create may be used to serialize and deserialize a Regexp object; see Marshal.

Method Regexp#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/regexp'
x = /foo/.as_json
# => {"json_class"=>"Regexp", "o"=>0, "s"=>"foo"}

Method JSON.create deserializes such a hash, returning a Regexp object:

Regexp.json_create(x) # => /foo/

Returns a JSON string representing self:

require 'json/add/regexp'
puts /foo/.to_json

Output:

{"json_class":"Regexp","o":0,"s":"foo"}

Returns object if it is a regexp:

Regexp.try_convert(/re/) # => /re/

Otherwise if object responds to :to_regexp, calls object.to_regexp and returns the result.

Returns nil if object does not respond to :to_regexp.

Regexp.try_convert('re') # => nil

Raises an exception unless object.to_regexp returns a regexp.

Returns true if matching against re can be done in linear time to the input string.

Regexp.linear_time?(/re/) # => true

Note that this is a property of the ruby interpreter, not of the argument regular expression. Identical regexp can or cannot run in linear time depending on your ruby binary. Neither forward nor backward compatibility is guaranteed about the return value of this method. Our current algorithm is (*1) but this is subject to change in the future. Alternative implementations can also behave differently. They might always return false for everything.

(*1): doi.org/10.1109/SP40001.2021.00032

See as_json.

Methods Set#as_json and Set.json_create may be used to serialize and deserialize a Set object; see Marshal.

Method Set#as_json serializes self, returning a 2-element hash representing self:

require 'json/add/set'
x = Set.new(%w/foo bar baz/).as_json
# => {"json_class"=>"Set", "a"=>["foo", "bar", "baz"]}

Method JSON.create deserializes such a hash, returning a Set object:

Set.json_create(x) # => #<Set: {"foo", "bar", "baz"}>
Search took: 7ms  ·  Total Results: 5313