Results for: "String#[]"

Returns a string representation of self:

Complex.rect(2).inspect                      # => "(2+0i)"
Complex.rect(-8, 6).inspect                  # => "(-8+6i)"
Complex.rect(0, Rational(1, 2)).inspect      # => "(0+(1/2)*i)"
Complex.rect(0, Float::INFINITY).inspect     # => "(0+Infinity*i)"
Complex.rect(Float::NAN, Float::NAN).inspect # => "(NaN+NaN*i)"

Returns true if both self.real.finite? and self.imag.finite? are true, false otherwise:

Complex.rect(1, 1).finite?               # => true
Complex.rect(Float::INFINITY, 0).finite? # => false

Related: Numeric#finite?, Float#finite?.

Returns string 'nil':

nil.inspect # => "nil"

Returns zero if self is positive, Math::PI otherwise.

Returns array [self, 0].

Returns the remainder after dividing self by other.

Of the Core and Standard Library classes, only Float and Rational use this implementation.

Examples:

11.0.remainder(4)              # => 3.0
11.0.remainder(-4)             # => 3.0
-11.0.remainder(4)             # => -3.0
-11.0.remainder(-4)            # => -3.0

12.0.remainder(4)              # => 0.0
12.0.remainder(-4)             # => 0.0
-12.0.remainder(4)             # => -0.0
-12.0.remainder(-4)            # => -0.0

13.0.remainder(4.0)            # => 1.0
13.0.remainder(Rational(4, 1)) # => 1.0

Rational(13, 1).remainder(4)   # => (1/1)
Rational(13, 1).remainder(-4)  # => (1/1)
Rational(-13, 1).remainder(4)  # => (-1/1)
Rational(-13, 1).remainder(-4) # => (-1/1)

Returns self truncated (toward zero) to a precision of digits decimal digits.

Numeric implements this by converting self to a Float and invoking Float#truncate.

Generates a sequence of numbers; with a block given, traverses the sequence.

Of the Core and Standard Library classes, Integer, Float, and Rational use this implementation.

A quick example:

squares = []
1.step(by: 2, to: 10) {|i| squares.push(i*i) }
squares # => [1, 9, 25, 49, 81]

The generated sequence:

If a block is given, calls the block with each number in the sequence; returns self. If no block is given, returns an Enumerator::ArithmeticSequence.

Keyword Arguments

With keyword arguments by and to, their values (or defaults) determine the step and limit:

# Both keywords given.
squares = []
4.step(by: 2, to: 10) {|i| squares.push(i*i) }    # => 4
squares # => [16, 36, 64, 100]
cubes = []
3.step(by: -1.5, to: -3) {|i| cubes.push(i*i*i) } # => 3
cubes   # => [27.0, 3.375, 0.0, -3.375, -27.0]
squares = []
1.2.step(by: 0.2, to: 2.0) {|f| squares.push(f*f) }
squares # => [1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0]

squares = []
Rational(6/5).step(by: 0.2, to: 2.0) {|r| squares.push(r*r) }
squares # => [1.0, 1.44, 1.9599999999999997, 2.5600000000000005, 3.24, 4.0]

# Only keyword to given.
squares = []
4.step(to: 10) {|i| squares.push(i*i) }           # => 4
squares # => [16, 25, 36, 49, 64, 81, 100]
# Only by given.

# Only keyword by given
squares = []
4.step(by:2) {|i| squares.push(i*i); break if i > 10 }
squares # => [16, 36, 64, 100, 144]

# No block given.
e = 3.step(by: -1.5, to: -3) # => (3.step(by: -1.5, to: -3))
e.class                      # => Enumerator::ArithmeticSequence

Positional Arguments

With optional positional arguments to and by, their values (or defaults) determine the step and limit:

squares = []
4.step(10, 2) {|i| squares.push(i*i) }    # => 4
squares # => [16, 36, 64, 100]
squares = []
4.step(10) {|i| squares.push(i*i) }
squares # => [16, 25, 36, 49, 64, 81, 100]
squares = []
4.step {|i| squares.push(i*i); break if i > 10 }  # => nil
squares # => [16, 25, 36, 49, 64, 81, 100, 121]

Implementation Notes

If all the arguments are integers, the loop operates using an integer counter.

If any of the arguments are floating point numbers, all are converted to floats, and the loop is executed floor(n + n*Float::EPSILON) + 1 times, where n = (limit - self)/step.

Returns true if self is an Integer.

1.0.integer? # => false
1.integer?   # => true

Returns true if self is a finite number, false otherwise.

Returns zero.

Returns the denominator (always positive).

Returns 0 if self is positive, Math::PI otherwise.

Returns self truncated (toward zero) to a precision of ndigits decimal digits.

When ndigits is positive, returns a float with ndigits digits after the decimal point (as available):

f = 12345.6789
f.truncate(1) # => 12345.6
f.truncate(3) # => 12345.678
f = -12345.6789
f.truncate(1) # => -12345.6
f.truncate(3) # => -12345.678

When ndigits is negative, returns an integer with at least ndigits.abs trailing zeros:

f = 12345.6789
f.truncate(0)  # => 12345
f.truncate(-3) # => 12000
f = -12345.6789
f.truncate(0)  # => -12345
f.truncate(-3) # => -12000

Note that the limited precision of floating-point arithmetic may lead to surprising results:

(0.3 / 0.1).truncate  #=> 2 (!)

Related: Float#round.

Returns true if self is not Infinity, -Infinity, or NaN, false otherwise:

f = 2.0      # => 2.0
f.finite?    # => true
f = 1.0/0.0  # => Infinity
f.finite?    # => false
f = -1.0/0.0 # => -Infinity
f.finite?    # => false
f = 0.0/0.0  # => NaN
f.finite?    # => false

Returns a string containing a representation of self; depending of the value of self, the string representation may contain:

Returns the denominator (always positive). The result is machine dependent.

See also Float#numerator.

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

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 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
No documentation available

Returns a string description of self:

Dir.new('example').inspect # => "#<Dir:example>"

Sets the position in self to zero; see Dir As Stream-Like:

dir = Dir.new('example')
dir.read    # => "."
dir.read    # => ".."
dir.pos     # => 2
dir.rewind  # => #<Dir:example>
dir.pos     # => 0

Removes the directory at dirpath from the underlying file system:

Dir.rmdir('foo') # => 0

Raises an exception if the directory is not empty.

Search took: 10ms  ·  Total Results: 4239