Returns the numerator.
Returns the denominator (always positive).
Returns a string containing the first character of self
:
s = 'foo' # => "foo" s.chr # => "f"
Returns an array of the characters in self
:
'hello'.chars # => ["h", "e", "l", "l", "o"] 'тест'.chars # => ["т", "е", "с", "т"] 'こんにちは'.chars # => ["こ", "ん", "に", "ち", "は"]
Concatenates each object in objects
to self
and returns self
:
s = 'foo' s.concat('bar', 'baz') # => "foobarbaz" s # => "foobarbaz"
For each given object object
that is an Integer
, the value is considered a codepoint and converted to a character before concatenation:
s = 'foo' s.concat(32, 'bar', 32, 'baz') # => "foo bar baz"
Related: String#<<
, which takes a single argument.
Returns a new string copied from self
, with trailing characters possibly removed.
Removes "\r\n"
if those are the last two characters.
"abc\r\n".chop # => "abc" "тест\r\n".chop # => "тест" "こんにちは\r\n".chop # => "こんにちは"
Otherwise removes the last character if it exists.
'abcd'.chop # => "abc" 'тест'.chop # => "тес" 'こんにちは'.chop # => "こんにち" ''.chop # => ""
If you only need to remove the newline separator at the end of the string, String#chomp
is a better alternative.
Returns a new string copied from self
, with trailing characters possibly removed:
When line_sep
is "\n"
, removes the last one or two characters if they are "\r"
, "\n"
, or "\r\n"
(but not "\n\r"
):
$/ # => "\n" "abc\r".chomp # => "abc" "abc\n".chomp # => "abc" "abc\r\n".chomp # => "abc" "abc\n\r".chomp # => "abc\n" "тест\r\n".chomp # => "тест" "こんにちは\r\n".chomp # => "こんにちは"
When line_sep
is ''
(an empty string), removes multiple trailing occurrences of "\n"
or "\r\n"
(but not "\r"
or "\n\r"
):
"abc\n\n\n".chomp('') # => "abc" "abc\r\n\r\n\r\n".chomp('') # => "abc" "abc\n\n\r\n\r\n\n\n".chomp('') # => "abc" "abc\n\r\n\r\n\r".chomp('') # => "abc\n\r\n\r\n\r" "abc\r\r\r".chomp('') # => "abc\r\r\r"
When line_sep
is neither "\n"
nor ''
, removes a single trailing line separator if there is one:
'abcd'.chomp('d') # => "abc" 'abcdd'.chomp('d') # => "abcd"
Like String#chop
, but modifies self
in place; returns nil
if self
is empty, self
otherwise.
Related: String#chomp!
.
Like String#chomp
, but modifies self
in place; returns nil
if no modification made, self
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 less than 0, false
otherwise.
Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595 d = 0.3.denominator #=> 18014398509481984 n.fdiv(d) #=> 0.3
See also Float#denominator
.
Returns the denominator (always positive). The result is machine dependent.
See also Float#numerator
.
Returns a simpler approximation of the value (flt-|eps| <= result <= flt+|eps|). If the optional argument eps
is not given, it will be chosen automatically.
0.3.rationalize #=> (3/10) 1.333.rationalize #=> (1333/1000) 1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r
.
Returns the Fiber
scheduler, that was last set for the current thread with Fiber.set_scheduler
. Returns nil
if no scheduler is set (which is the default), and non-blocking fibers’ behavior is the same as blocking. (see “Non-blocking fibers” section in class docs for details about the scheduler concept).
The method is expected to immediately run the provided block of code in a separate non-blocking fiber.
puts "Go to sleep!" Fiber.set_scheduler(MyScheduler.new) Fiber.schedule do puts "Going to sleep" sleep(1) puts "I slept well" end puts "Wakey-wakey, sleepyhead"
Assuming MyScheduler is properly implemented, this program will produce:
Go to sleep! Going to sleep Wakey-wakey, sleepyhead ...1 sec pause here... I slept well
…e.g. on the first blocking operation inside the Fiber
(sleep(1)
), the control is yielded to the outside code (main fiber), and at the end of that execution, the scheduler takes care of properly resuming all the blocked fibers.
Note that the behavior described above is how the method is expected to behave, actual behavior is up to the current scheduler’s implementation of Fiber::Scheduler#fiber
method. Ruby doesn’t enforce this method to behave in any particular way.
If the scheduler is not set, the method raises RuntimeError (No scheduler is available!)
.
Calls the block with each entry name in the directory at dirpath
; sets the given encoding onto each passed entry_name
:
Dir.foreach('/example') {|entry_name| p entry_name }
Output:
"config.h" "lib" "main.rb" ".." "."
Encoding:
Dir.foreach('/example') {|entry_name| p entry_name.encoding; break } Dir.foreach('/example', encoding: 'US-ASCII') {|entry_name| p entry_name.encoding; break }
Output:
#<Encoding:UTF-8> #<Encoding:US-ASCII>
See String Encoding.
Returns an enumerator if no block is given.
Returns an array of the entry names in the directory at dirpath
except for '.'
and '..'
; sets the given encoding onto each returned entry name:
Dir.children('/example') # => ["config.h", "lib", "main.rb"] Dir.children('/example').first.encoding # => #<Encoding:UTF-8> Dir.children('/example', encoding: 'US-ASCII').first.encoding # => #<Encoding:US-ASCII>
See String Encoding.
Raises an exception if the directory does not exist.
Returns the dirpath
string that was used to create self
(or nil
if created by method Dir.for_fd
):
Dir.new('example').path # => "example"
Calls the block with each entry name in self
:
Dir.new('example').each {|entry_name| p entry_name }
Output:
"." ".." "config.h" "lib" "main.rb"
With no block given, returns an Enumerator
.
Returns an array of the entry names in self
except for '.'
and '..'
:
dir = Dir.new('/example') dir.children # => ["config.h", "lib", "main.rb"]
Changes the current working directory to self
:
Dir.pwd # => "/" dir = Dir.new('example') dir.chdir Dir.pwd # => "/example"
With a block, temporarily changes the working directory:
Calls the block.
Changes to the given directory.
Executes the block (yields no args).
Restores the previous working directory.
Returns the block’s return value.
Uses Dir.fchdir
if available, and Dir.chdir
if not, see those methods for caveats.
Changes the current working directory to the directory specified by the integer file descriptor fd
.
When passing a file descriptor over a UNIX socket or to a child process, using fchdir
instead of chdir
avoids the time-of-check to time-of-use vulnerability
With no block, changes to the directory given by fd
:
Dir.chdir('/var/spool/mail') Dir.pwd # => "/var/spool/mail" dir = Dir.new('/usr') fd = dir.fileno Dir.fchdir(fd) Dir.pwd # => "/usr"
With a block, temporarily changes the working directory:
Calls the block with the argument.
Changes to the given directory.
Executes the block (yields no args).
Restores the previous working directory.
Returns the block’s return value.
Example:
Dir.chdir('/var/spool/mail') Dir.pwd # => "/var/spool/mail" dir = Dir.new('/tmp') fd = dir.fileno Dir.fchdir(fd) do Dir.pwd # => "/tmp" end Dir.pwd # => "/var/spool/mail"
This method uses the fchdir() function defined by POSIX 2008; the method is not implemented on non-POSIX platforms (raises NotImplementedError
).
Raises an exception if the file descriptor is not valid.
In a multi-threaded program an error is raised if a thread attempts to open a chdir
block while another thread has one open, or a call to chdir
without a block occurs inside a block passed to chdir
(even in the same thread).
Changes the current working directory.
With argument new_dirpath
and no block, changes to the given dirpath
:
Dir.pwd # => "/example" Dir.chdir('..') # => 0 Dir.pwd # => "/"
With no argument and no block:
Changes to the value of environment variable HOME
if defined.
Otherwise changes to the value of environment variable LOGDIR
if defined.
Otherwise makes no change.
With argument new_dirpath
and a block, temporarily changes the working directory:
Calls the block with the argument.
Changes to the given directory.
Executes the block (yielding the new path).
Restores the previous working directory.
Returns the block’s return value.
Example:
Dir.chdir('/var/spool/mail') Dir.pwd # => "/var/spool/mail" Dir.chdir('/tmp') do Dir.pwd # => "/tmp" end Dir.pwd # => "/var/spool/mail"
With no argument and a block, calls the block with the current working directory (string) and returns the block’s return value.
Calls to Dir.chdir with blocks may be nested:
Dir.chdir('/var/spool/mail') Dir.pwd # => "/var/spool/mail" Dir.chdir('/tmp') do Dir.pwd # => "/tmp" Dir.chdir('/usr') do Dir.pwd # => "/usr" end Dir.pwd # => "/tmp" end Dir.pwd # => "/var/spool/mail"
In a multi-threaded program an error is raised if a thread attempts to open a chdir
block while another thread has one open, or a call to chdir
without a block occurs inside a block passed to chdir
(even in the same thread).
Raises an exception if the target directory does not exist.