Results for: "to_proc"

Creates a new Enumerator which will enumerate by calling method on obj, passing args if any.

If a block is given, it will be used to calculate the size of the enumerator without the need to iterate it (see Enumerator#size).

Examples

str = "xyz"

enum = str.enum_for(:each_byte)
enum.each { |b| puts b }
# => 120
# => 121
# => 122

# protect an array from being modified by some_method
a = [1, 2, 3]
some_method(a.to_enum)

It is typical to call to_enum when defining methods for a generic Enumerable, in case no block is passed.

Here is such an example, with parameter passing and a sizing block:

module Enumerable
  # a generic method to repeat the values of any enumerable
  def repeat(n)
    raise ArgumentError, "#{n} is negative!" if n < 0
    unless block_given?
      return to_enum(__method__, n) do # __method__ is :repeat here
        sz = size     # Call size and multiply by n...
        sz * n if sz  # but return nil if size itself is nil
      end
    end
    each do |*val|
      n.times { yield *val }
    end
  end
end

%i[hello world].repeat(2) { |w| puts w }
  # => Prints 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
  # => returns an Enumerator when called without a block
enum.first(4) # => [1, 1, 1, 2]
enum.size # => 42

Convert an object to YAML. See Psych.dump for more information on the available options.

Casts an Integer as an OpenSSL::BN

See ‘man bn` for more info.

Since int is already an Integer, returns self.

to_int is an alias for to_i.

Stores class name (Complex) along with real value r and imaginary value i as JSON string

Invokes the child class’s to_i method to convert num to an integer.

1.0.class          #=> Float
1.0.to_int.class   #=> Integer
1.0.to_i.class     #=> Integer

Returns self.

If called on a subclass of String, converts the receiver to a String object.

Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.

"Koala".intern         #=> :Koala
s = 'cat'.to_sym       #=> :cat
s == :cat              #=> true
s = '@cat'.to_sym      #=> :@cat
s == :@cat             #=> true

This can also be used to create symbols that cannot be represented using the :xxx notation.

'cat and dog'.to_sym   #=> :"cat and dog"

Returns the float truncated to an Integer.

1.2.to_i      #=> 1
(-1.2).to_i   #=> -1

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

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

to_int is an alias for to_i.

Returns the path parameter passed to dir’s constructor.

d = Dir.new("..")
d.path   #=> ".."

Returns the pathname used to create file as a string. Does not normalize the name.

The pathname may not point to the file corresponding to file. For instance, the pathname becomes void when the file has been moved or deleted.

This method raises IOError for a file created using File::Constants::TMPFILE because they don’t have a pathname.

File.new("testfile").path               #=> "testfile"
File.new("/tmp/../tmp/xxx", "w").path   #=> "/tmp/../tmp/xxx"

Returns true if exception messages will be sent to a tty.

Stores class name (Exception) with message m and backtrace array b as JSON string

Returns the value as an Integer.

If the BigDecimal is infinity or NaN, raises FloatDomainError.

Converts a BigDecimal to a String of the form “nnnnnn.mmm”. This method is deprecated; use BigDecimal#to_s(“F”) instead.

require 'bigdecimal/util'

d = BigDecimal("3.14")
d.to_digits                  # => "3.14"

return the JSON value

Stores class name (Rational) along with numerator value n and denominator value d as JSON string

Returns a Time object which denotes self.

Returns self.

Returns a DateTime object which denotes self.

Stores class name (Date) with Julian year y, month m, day d and Day of Calendar Reform sg as JSON string

Returns a Time object which denotes self.

Returns a Date object which denotes self.

Returns self.

Stores class name (DateTime) with Julian year y, month m, day d, hour H, minute M, second S, offset of and Day of Calendar Reform sg as JSON string

Search took: 4ms  ·  Total Results: 1909