Returns the denominator (always positive).
Returns a string containing the first character of self
:
s = 'foo' # => "foo" s.chr # => "f"
Returns an array of characters in str. This is a shorthand for str.each_char.to_a
.
If a block is given, which is a deprecated form, works the same as each_char
.
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
with the last character removed. If the string ends with \r\n
, both characters are removed. Applying chop
to an empty string returns an empty string. String#chomp
is often a safer alternative, as it leaves the string unchanged if it doesn’t end in a record separator.
"string\r\n".chop #=> "string" "string\n\r".chop #=> "string\n" "string\n".chop #=> "string" "string".chop #=> "strin" "x".chop.chop #=> ""
Returns a new String
with the given record separator removed from the end of str (if present). If $/
has not been changed from the default Ruby record separator, then chomp
also removes carriage return characters (that is, it will remove \n
, \r
, and \r\n
). If $/
is an empty string, it will remove all trailing newlines from the string.
"hello".chomp #=> "hello" "hello\n".chomp #=> "hello" "hello\r\n".chomp #=> "hello" "hello\n\r".chomp #=> "hello\n" "hello\r".chomp #=> "hello" "hello \n there".chomp #=> "hello \n there" "hello".chomp("llo") #=> "he" "hello\r\n\r\n".chomp('') #=> "hello" "hello\r\n\r\r\n".chomp('') #=> "hello\r\n\r"
Processes str as for String#chop
, returning str, or nil
if str is the empty string. See also String#chomp!
.
Modifies str in place as described for String#chomp
, returning str, or nil
if no modifications were made.
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 float
is less than 0.
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::SchedulerInterface#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 once for each entry in the named directory, passing the filename of each entry as a parameter to the block.
If no block is given, an enumerator is returned instead.
Dir.foreach("testdir") {|x| puts "Got #{x}" }
produces:
Got . Got .. Got config.h Got main.rb
Returns an array containing all of the filenames except for “.” and “..” in the given directory. Will raise a SystemCallError
if the named directory doesn’t exist.
The optional encoding keyword argument specifies the encoding of the directory. If not specified, the filesystem encoding is used.
Dir.children("testdir") #=> ["config.h", "main.rb"]
Calls the block once for each entry in this directory, passing the filename of each entry as a parameter to the block.
If no block is given, an enumerator is returned instead.
d = Dir.new("testdir") d.each {|x| puts "Got #{x}" }
produces:
Got . Got .. Got config.h Got main.rb
Returns an array containing all of the filenames except for “.” and “..” in this directory.
d = Dir.new("testdir") d.children #=> ["config.h", "main.rb"]
Changes the current working directory of the process to the given string. When called without an argument, changes the directory to the value of the environment variable HOME
, or LOGDIR
. SystemCallError
(probably Errno::ENOENT) if the target directory does not exist.
If a block is given, it is passed the name of the new current directory, and the block is executed with that as the current directory. The original working directory is restored when the block exits. The return value of chdir
is the value of the block. chdir
blocks can be nested, but in a multi-threaded program an error will be 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).
Dir.chdir("/var/spool/mail") puts Dir.pwd Dir.chdir("/tmp") do puts Dir.pwd Dir.chdir("/usr") do puts Dir.pwd end puts Dir.pwd end puts Dir.pwd
produces:
/var/spool/mail /tmp /usr /tmp /var/spool/mail
Changes this process’s idea of the file system root. Only a privileged process may make this call. Not available on all platforms. On Unix systems, see chroot(2)
for more information.
Returns a File::Stat
object for the named file (see File::Stat
).
File.stat("testfile").mtime #=> Tue Apr 08 12:58:04 CDT 2003
Same as File::stat
, but does not follow the last symbolic link. Instead, reports on the link itself.
File.symlink("testfile", "link2test") #=> 0 File.stat("testfile").size #=> 66 File.lstat("link2test").size #=> 8 File.stat("link2test").size #=> 66