Returns the change time for file (that is, the time directory information about the file was changed, not the file itself).
Note that on Windows (NTFS), returns creation time (birth time).
File.new("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003
Truncates file to at most integer bytes. The file must be opened for writing. Not available on all platforms.
f = File.new("out", "w") f.syswrite("1234567890") #=> 10 f.truncate(5) #=> 0 f.close() #=> nil File.size("out") #=> 5
Returns a replicated encoding of enc whose name is name. The new encoding should have the same byte structure of enc. If name is used by another encoding, raise ArgumentError
.
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
or:
MyClass = DelegateClass(ClassToDelegateTo) do # 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
Returns the result of invoking exception.to_s
. Normally this returns the exception’s message or name.
Return the status value associated with this system exit.
Return the matchee associated with this NoMatchingPatternKeyError
exception.
Returns an array of modules defined within the receiver.
module A refine Integer do end refine String do end end p A.refinements
produces:
[#<refinement:Integer@A>, #<refinement:String@A>]
The first form is equivalent to attr_reader
. The second form is equivalent to attr_accessor(name)
but deprecated. The last form is equivalent to attr_reader(name)
but deprecated. Returns an array of defined method names as symbols.
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. An Array
of Symbols and/or Strings is also accepted. If a single argument is passed, it is returned. If no argument is passed, nil is returned. If multiple arguments are passed, the arguments are returned as an array.
module Mod def a() end def b() end private def c() end private :a end Mod.private_instance_methods #=> [:a, :c]
Note that to show a private method on RDoc
, use :doc:
.
Returns True if the value is Not a Number.
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 true
if rat
is less than 0.
Returns rat
truncated (toward zero) to a precision of ndigits
decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs
trailing zeros.
Returns a rational when ndigits
is positive, otherwise returns an integer.
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 Rational('-123.456').truncate(+1).to_f #=> -123.4 Rational('-123.456').truncate(-1) #=> -120
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 formed fom the arguments.
With no arguments, returns the date for January 1, -4712:
Date.ordinal.to_s # => "-4712-01-01"
With argument year
, returns the date for January 1 of that year:
Date.ordinal(2001).to_s # => "2001-01-01" Date.ordinal(-2001).to_s # => "-2001-01-01"
With positive argument yday
== n
, returns the date for the nth
day of the given year:
Date.ordinal(2001, 14).to_s # => "2001-01-14"
With negative argument yday
, counts backward from the end of the year:
Date.ordinal(2001, -14).to_s # => "2001-12-18"
Raises an exception if yday
is zero or out of range.
See argument start.
Returns a new Date object constructed from the arguments.
Argument cwyear
gives the year, and should be an integer.
Argument cweek
gives the index of the week within the year, and should be in range (1..53) or (-53..-1); in some years, 53 or -53 will be out-of-range; if negative, counts backward from the end of the year:
Date.commercial(2022, 1, 1).to_s # => "2022-01-03" Date.commercial(2022, 52, 1).to_s # => "2022-12-26"
Argument cwday
gives the indes of the weekday within the week, and should be in range (1..7) or (-7..-1); 1 or -7 is Monday; if negative, counts backward from the end of the week:
Date.commercial(2022, 1, 1).to_s # => "2022-01-03" Date.commercial(2022, 1, -7).to_s # => "2022-01-03"
When cweek
is 1:
If January 1 is a Friday, Saturday, or Sunday, the first week begins in the week after:
Date::ABBR_DAYNAMES[Date.new(2023, 1, 1).wday] # => "Sun" Date.commercial(2023, 1, 1).to_s # => "2023-01-02" Date.commercial(2023, 1, 7).to_s # => "2023-01-08"
Otherwise, the first week is the week of January 1, which may mean some of the days fall on the year before:
Date::ABBR_DAYNAMES[Date.new(2020, 1, 1).wday] # => "Wed" Date.commercial(2020, 1, 1).to_s # => "2019-12-30" Date.commercial(2020, 1, 7).to_s # => "2020-01-05"
See argument start.
Related: Date.jd
, Date.new
, Date.ordinal
.
Returns a hash of values parsed from string
according to the given format
:
Date._strptime('2001-02-03', '%Y-%m-%d') # => {:year=>2001, :mon=>2, :mday=>3}
For other formats, see Formats for Dates and Times. (Unlike Date.strftime
, does not support flags and width.)
See also strptime(3).
Related: Date.strptime
(returns a Date object).
Returns a new Date object with values parsed from string
, according to the given format
:
Date.strptime('2001-02-03', '%Y-%m-%d') # => #<Date: 2001-02-03> Date.strptime('03-02-2001', '%d-%m-%Y') # => #<Date: 2001-02-03> Date.strptime('2001-034', '%Y-%j') # => #<Date: 2001-02-03> Date.strptime('2001-W05-6', '%G-W%V-%u') # => #<Date: 2001-02-03> Date.strptime('2001 04 6', '%Y %U %w') # => #<Date: 2001-02-03> Date.strptime('2001 05 6', '%Y %W %u') # => #<Date: 2001-02-03> Date.strptime('sat3feb01', '%a%d%b%y') # => #<Date: 2001-02-03>
For other formats, see Formats for Dates and Times. (Unlike Date.strftime
, does not support flags and width.)
See argument start.
See also strptime(3).
Related: Date._strptime
(returns a hash).
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 a hash of values parsed from string
, which should be a valid HTTP date format:
d = Date.new(2001, 2, 3) s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT" Date._httpdate(s) # => {:wday=>6, :mday=>3, :mon=>2, :year=>2001, :hour=>0, :min=>0, :sec=>0, :zone=>"GMT", :offset=>0}
Related: Date.httpdate
(returns a Date object).
Returns a new Date object with values parsed from string
, which should be a valid HTTP date format:
d = Date.new(2001, 2, 3) s = d.httpdate # => "Sat, 03 Feb 2001 00:00:00 GMT" Date.httpdate(s) # => #<Date: 2001-02-03>
See:
Argument start.
Argument limit.
Related: Date._httpdate
(returns a hash).
Returns the astronomical modified Julian day number. This is a fractional number, which is not adjusted by the offset.
DateTime.new(2001,2,3,4,5,6,'+7').amjd #=> (249325817/4800) DateTime.new(2001,2,2,14,5,6,'-7').amjd #=> (249325817/4800)
Returns the month in range (1..12):
Date.new(2001, 2, 3).mon # => 2
Date#month
is an alias for Date#mon
.