Returns the result of interpreting enum as a list of [key, value]
pairs.
%i[hello world].each_with_index.to_h # => {:hello => 0, :world => 1}
Returns self
.
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
).
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
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 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.
File.new("testfile").path #=> "testfile" File.new("/tmp/../tmp/xxx", "w").path #=> "/tmp/../tmp/xxx"
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.new("3.14") d.to_digits # => "3.14"
return the JSON
value
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.