Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). If the optional argument eps
is not given, it will be chosen automatically.
0.3.rationalize #=> (3/10) 1.333.rationalize #=> (1333/1000) 1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r
.
Returns the current fiber. If you are not running in the context of a fiber this method will return the root fiber.
Returns a copy of the storage hash for the fiber. The method can only be called on the Fiber.current
.
Sets the storage hash for the fiber. This feature is experimental and may change in the future. The method can only be called on the Fiber.current
.
You should be careful about using this method as you may inadvertently clear important fiber-storage state. You should mostly prefer to assign specific keys in the storage using Fiber::[]=
.
You can also use Fiber.new(storage: nil)
to create a fiber with an empty storage.
Example:
while request = request_queue.pop # Reset the per-request state: Fiber.current.storage = nil handle_request(request) end
Raises an exception in the fiber at the point at which the last Fiber.yield
was called. If the fiber has not been started or has already run to completion, raises FiberError
. If the fiber is yielding, it is resumed. If it is transferring, it is transferred into. But if it is resuming, raises FiberError
.
With no arguments, raises a RuntimeError
. With a single String
argument, raises a RuntimeError
with the string as a message. Otherwise, the first parameter should be the name of an Exception
class (or an object that returns an Exception
object when sent an exception
message). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the rescue
clause of begin...end
blocks.
Raises FiberError
if called on a Fiber
belonging to another Thread
.
See Kernel#raise
for more information.
Returns the current execution stack of the fiber. start
, count
and end
allow to select only parts of the backtrace.
def level3 Fiber.yield end def level2 level3 end def level1 level2 end f = Fiber.new { level1 } # It is empty before the fiber started f.backtrace #=> [] f.resume f.backtrace #=> ["test.rb:2:in `yield'", "test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"] p f.backtrace(1) # start from the item 1 #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'", "test.rb:13:in `block in <main>'"] p f.backtrace(2, 2) # start from item 2, take 2 #=> ["test.rb:6:in `level2'", "test.rb:10:in `level1'"] p f.backtrace(1..3) # take items from 1 to 3 #=> ["test.rb:2:in `level3'", "test.rb:6:in `level2'", "test.rb:10:in `level1'"] f.resume # It is nil after the fiber is finished f.backtrace #=> nil
Transfer control to another fiber, resuming it from where it last stopped or starting it if it was not resumed before. The calling fiber will be suspended much like in a call to Fiber.yield
.
The fiber which receives the transfer call treats it much like a resume call. Arguments passed to transfer are treated like those passed to resume.
The two style of control passing to and from fiber (one is resume
and Fiber::yield
, another is transfer
to and from fiber) can’t be freely mixed.
If the Fiber’s lifecycle had started with transfer, it will never be able to yield or be resumed control passing, only finish or transfer back. (It still can resume other fibers that are allowed to be resumed.)
If the Fiber’s lifecycle had started with resume, it can yield or transfer to another Fiber
, but can receive control back only the way compatible with the way it was given away: if it had transferred, it only can be transferred back, and if it had yielded, it only can be resumed back. After that, it again can transfer or yield.
If those rules are broken FiberError
is raised.
For an individual Fiber
design, yield/resume is easier to use (the Fiber
just gives away control, it doesn’t need to think about who the control is given to), while transfer is more flexible for complex cases, allowing to build arbitrary graphs of Fibers dependent on each other.
Example:
manager = nil # For local var to be visible inside worker block # This fiber would be started with transfer # It can't yield, and can't be resumed worker = Fiber.new { |work| puts "Worker: starts" puts "Worker: Performed #{work.inspect}, transferring back" # Fiber.yield # this would raise FiberError: attempt to yield on a not resumed fiber # manager.resume # this would raise FiberError: attempt to resume a resumed fiber (double resume) manager.transfer(work.capitalize) } # This fiber would be started with resume # It can yield or transfer, and can be transferred # back or resumed manager = Fiber.new { puts "Manager: starts" puts "Manager: transferring 'something' to worker" result = worker.transfer('something') puts "Manager: worker returned #{result.inspect}" # worker.resume # this would raise FiberError: attempt to resume a transferring fiber Fiber.yield # this is OK, the fiber transferred from and to, now it can yield puts "Manager: finished" } puts "Starting the manager" manager.resume puts "Resuming the manager" # manager.transfer # this would raise FiberError: attempt to transfer to a yielding fiber manager.resume
produces
Starting the manager Manager: starts Manager: transferring 'something' to worker Worker: starts Worker: Performed "something", transferring back Manager: worker returned "Something" Resuming the manager Manager: finished
Returns true
if filepath
points to a character device, false
otherwise.
File.chardev?($stdin) # => true File.chardev?('t.txt') # => false
Writes self
on the given port:
1.display "cat".display [ 4, 5, 6 ].display puts
Output:
1cat[4, 5, 6]
Returns the backtrace (the list of code locations that led to the exception), as an array of strings.
Example (assuming the code is stored in the file named t.rb
):
def division(numerator, denominator) numerator / denominator end begin division(1, 0) rescue => ex p ex.backtrace # ["t.rb:2:in 'Integer#/'", "t.rb:2:in 'Object#division'", "t.rb:6:in '<main>'"] loc = ex.backtrace.first p loc.class # String end
The value returned by this method migth be adjusted when raising (see Kernel#raise
), or during intermediate handling by set_backtrace
.
See also backtrace_locations
that provide the same value, as structured objects. (Note though that two values might not be consistent with each other when backtraces are manually adjusted.)
see Backtraces.
Return the arguments passed in as the third parameter to the constructor.
Return this SystemCallError’s error number.
Parse an HTTP query string into a hash of key=>value pairs.
params = CGI.parse("query_string") # {"name1" => ["value1", "value2", ...], # "name2" => ["value1", "value2", ...], ... }
Returns a new Date object constructed from the present date:
Date.today.to_s # => "2022-07-06"
See argument start.
Note: This method recognizes many forms in string
, but it is not a validator. For formats, see “Specialized Format Strings” in Formats for Dates and Times
If string
does not specify a valid date, the result is unpredictable; consider using Date._strptime
instead.
Returns a hash of values parsed from string
:
Date._parse('2001-02-03') # => {:year=>2001, :mon=>2, :mday=>3}
If comp
is true
and the given year is in the range (0..99)
, the current century is supplied; otherwise, the year is taken as given:
Date._parse('01-02-03', true) # => {:year=>2001, :mon=>2, :mday=>3} Date._parse('01-02-03', false) # => {:year=>1, :mon=>2, :mday=>3}
See argument limit.
Related: Date.parse
(returns a Date object).
Note: This method recognizes many forms in string
, but it is not a validator. For formats, see “Specialized Format Strings” in Formats for Dates and Times If string
does not specify a valid date, the result is unpredictable; consider using Date._strptime
instead.
Returns a new Date object with values parsed from string
:
Date.parse('2001-02-03') # => #<Date: 2001-02-03> Date.parse('20010203') # => #<Date: 2001-02-03> Date.parse('3rd Feb 2001') # => #<Date: 2001-02-03>
If comp
is true
and the given year is in the range (0..99)
, the current century is supplied; otherwise, the year is taken as given:
Date.parse('01-02-03', true) # => #<Date: 2001-02-03> Date.parse('01-02-03', false) # => #<Date: 0001-02-03>
See:
Argument start.
Argument limit.
Related: Date._parse
(returns a hash).
Returns commercial-date year for self
(see Date.commercial
):
Date.new(2001, 2, 3).cwyear # => 2001 Date.new(2000, 1, 1).cwyear # => 1999
Returns the commercial-date weekday index for self
(see Date.commercial
); 1 is Monday:
Date.new(2001, 2, 3).cwday # => 6
Returns true
if self
is a Sunday, false
otherwise.
Returns true
if self
is a Monday, false
otherwise.