Iterates over the block according to how this Enumerator
was constructed. If no block and no arguments are given, returns self.
"Hello, world!".scan(/\w+/) #=> ["Hello", "world"] "Hello, world!".to_enum(:scan, /\w+/).to_a #=> ["Hello", "world"] "Hello, world!".to_enum(:scan).each(/\w+/).to_a #=> ["Hello", "world"] obj = Object.new def obj.each_arg(a, b=:b, *rest) yield a yield b yield rest :method_returned end enum = obj.to_enum :each_arg, :a, :x enum.each.to_a #=> [:a, :x, []] enum.each.equal?(enum) #=> true enum.each { |elm| elm } #=> :method_returned enum.each(:y, :z).to_a #=> [:a, :x, [:y, :z]] enum.each(:y, :z).equal?(enum) #=> false enum.each(:y, :z) { |elm| elm } #=> :method_returned
The primary interface to this library. Use to setup delegation when defining your class.
class MyClass < DelegateClass(ClassToDelegateTo) # Step 1 def initialize super(obj_of_ClassToDelegateTo) # Step 2 end end
Here’s a sample of use from Tempfile
which is really a File
object with a few special rules about storage location and when the File
should be deleted. That makes for an almost textbook perfect example of how to use delegation.
class Tempfile < DelegateClass(File) # constant and class member data initialization... def initialize(basename, tmpdir=Dir::tmpdir) # build up file path/name in var tmpname... @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600) # ... super(@tmpfile) # below this point, all methods of File are supported... end # ... end
Return the status value associated with this system exit.
With no arguments, sets the default visibility for subsequently defined methods to private. With arguments, sets the named methods to have private visibility. String arguments are converted to symbols.
module Mod def a() end def b() end private def c() end private :a end Mod.private_instance_methods #=> [:a, :c]
Returns the remainder from dividing by the value.
x.remainder(y) means x-y*(x/y).truncate
Truncate to the nearest integer (by default), returning the result as a BigDecimal
.
BigDecimal('3.14159').truncate #=> 3 BigDecimal('8.7').truncate #=> 8 BigDecimal('-9.9').truncate #=> -9
If n is specified and positive, the fractional part of the result has no more than that many digits.
If n is specified and negative, at least that many digits to the left of the decimal point will be 0 in the result.
BigDecimal('3.14159').truncate(3) #=> 3.141 BigDecimal('13345.234').truncate(-2) #=> 13300.0
Returns a string containing the character represented by the int
‘s value according to encoding
.
65.chr #=> "A" 230.chr #=> "\346" 255.chr(Encoding::UTF_8) #=> "\303\277"
As int
is already an Integer
, all these methods simply return the receiver.
Returns self.
Returns 1.
Returns the value as a rational. The optional argument eps is always ignored.
Returns the numerator.
Rational(7).numerator #=> 7 Rational(7, 1).numerator #=> 7 Rational(9, -4).numerator #=> -9 Rational(-2, -10).numerator #=> 1
Returns the denominator (always positive).
Rational(7).denominator #=> 1 Rational(7, 1).denominator #=> 1 Rational(9, -4).denominator #=> 4 Rational(-2, -10).denominator #=> 5 rat.numerator.gcd(rat.denominator) #=> 1
Returns the truncated value (toward zero).
Rational(3).truncate #=> 3 Rational(2, 3).truncate #=> 0 Rational(-3, 2).truncate #=> -1 # decimal - 1 2 3 . 4 5 6 # ^ ^ ^ ^ ^ ^ # precision -3 -2 -1 0 +1 +2 '%f' % Rational('-123.456').truncate(+1) #=> "-123.400000" '%f' % Rational('-123.456').truncate(-1) #=> "-120.000000"
Returns a simpler approximation of the value if the optional argument eps is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
r = Rational(5033165, 16777216) r.rationalize #=> (5033165/16777216) r.rationalize(Rational('0.01')) #=> (3/10) r.rationalize(Rational('0.1')) #=> (1/3)
Returns a hash of parsed elements.
Creates a new Date
object by parsing from a string according to some RFC 2616 format.
Date.httpdate('Sat, 03 Feb 2001 00:00:00 GMT') #=> #<Date: 2001-02-03 ...>
Returns true if the date is Saturday.
This method is equivalent to strftime(‘%a, %d %b %Y %T GMT’). See also RFC 2616.
Creates a new Date
object by parsing from a string according to some RFC 2616 format.
DateTime.httpdate('Sat, 03 Feb 2001 04:05:06 GMT') #=> #<DateTime: 2001-02-03T04:05:06+00:00 ...>
Parses date
as an HTTP-date defined by RFC 2616 and converts it to a Time
object.
ArgumentError
is raised if date
is not compliant with RFC 2616 or if the Time
class cannot represent specified date.
See httpdate
for more information on this format.
You must require ‘time’ to use this method.
Returns a string which represents the time as RFC 1123 date of HTTP-date defined by RFC 2616:
day-of-week, DD month-name CCYY hh:mm:ss GMT
Note that the result is always UTC (GMT).
You must require ‘time’ to use this method.
Creates a new Time
object with the value given by time
, the given number of seconds_with_frac
, or seconds
and microseconds_with_frac
since the Epoch. seconds_with_frac
and microseconds_with_frac
can be an Integer
, Float
, Rational
, or other Numeric
. non-portable feature allows the offset to be negative on some systems.
If a numeric argument is given, the result is in local time.
Time.at(0) #=> 1969-12-31 18:00:00 -0600 Time.at(Time.at(0)) #=> 1969-12-31 18:00:00 -0600 Time.at(946702800) #=> 1999-12-31 23:00:00 -0600 Time.at(-284061600) #=> 1960-12-31 00:00:00 -0600 Time.at(946684800.2).usec #=> 200000 Time.at(946684800, 123456.789).nsec #=> 123456789
Creates a Time
object based on given values, interpreted as UTC (GMT). The year must be specified. Other values default to the minimum value for that field (and may be nil
or omitted). Months may be specified by numbers from 1 to 12, or by the three-letter English
month names. Hours are specified on a 24-hour clock (0..23). Raises an ArgumentError
if any values are out of range. Will also accept ten arguments in the order output by Time#to_a
.
sec_with_frac
and usec_with_frac
can have a fractional part.
Time.utc(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC Time.gm(2000,"jan",1,20,15,1) #=> 2000-01-01 20:15:01 UTC