Returns the result of interpreting leading characters in str as a floating point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0.0
is returned. This method never raises an exception.
"123.45e1".to_f #=> 1234.5 "45.67 degrees".to_f #=> 45.67 "thx1138".to_f #=> 0.0
Returns a new string with the characters from str in reverse order.
"stressed".reverse #=> "desserts"
Reverses str in place.
Prepend—Prepend the given strings to str.
a = "!" a.prepend("hello ", "world") #=> "hello world!" a #=> "hello world!"
See also String#concat
.
If integer is greater than the length of str, returns a new String
of length integer with str left justified and padded with padstr; otherwise, returns str.
"hello".ljust(4) #=> "hello" "hello".ljust(20) #=> "hello " "hello".ljust(20, '1234') #=> "hello123412341234123"
If integer is greater than the length of str, returns a new String
of length integer with str right justified and padded with padstr; otherwise, returns str.
"hello".rjust(4) #=> "hello" "hello".rjust(20) #=> " hello" "hello".rjust(20, '1234') #=> "123412341234123hello"
Returns a copy of the receiver with leading and trailing whitespace removed.
Whitespace is defined as any of the following characters: null, horizontal tab, line feed, vertical tab, form feed, carriage return, space.
" hello ".strip #=> "hello" "\tgoodbye\r\n".strip #=> "goodbye" "\x00\t\n\v\f\r ".strip #=> "" "hello".strip #=> "hello"
Returns a copy of the receiver with leading whitespace removed. See also String#rstrip
and String#strip
.
Refer to String#strip
for the definition of whitespace.
" hello ".lstrip #=> "hello " "hello".lstrip #=> "hello"
Returns a copy of the receiver with trailing whitespace removed. See also String#lstrip
and String#strip
.
Refer to String#strip
for the definition of whitespace.
" hello ".rstrip #=> " hello" "hello".rstrip #=> "hello"
Removes leading and trailing whitespace from the receiver. Returns the altered receiver, or nil
if there was no change.
Refer to String#strip
for the definition of whitespace.
" hello ".strip! #=> "hello" "hello".strip! #=> nil
Removes leading whitespace from the receiver. Returns the altered receiver, or nil
if no change was made. See also String#rstrip!
and String#strip!
.
Refer to String#strip
for the definition of whitespace.
" hello ".lstrip! #=> "hello " "hello ".lstrip! #=> nil "hello".lstrip! #=> nil
Removes trailing whitespace from the receiver. Returns the altered receiver, or nil
if no change was made. See also String#lstrip!
and String#strip!
.
Refer to String#strip
for the definition of whitespace.
" hello ".rstrip! #=> " hello" " hello".rstrip! #=> nil "hello".rstrip! #=> nil
Returns the value of float
as a BigDecimal
. The precision
parameter is used to determine the number of significant digits for the result (the default is Float::DIG
).
require 'bigdecimal' require 'bigdecimal/util' 0.5.to_d # => 0.5e0 1.234.to_d(2) # => 0.12e1
See also BigDecimal::new
.
Returns a string containing a representation of self
. As well as a fixed or exponential form of the float
, the call may return NaN
, Infinity
, and -Infinity
.
Since float
is already a Float
, returns self
.
Returns the float
truncated to an Integer
.
1.2.to_i #=> 1 (-1.2).to_i #=> -1
Note that the limited precision of floating point arithmetic might lead to surprising results:
(0.3 / 0.1).to_i #=> 2 (!)
Returns the largest number less than or equal to float
with 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 floating point number when ndigits
is positive, otherwise returns an integer.
1.2.floor #=> 1 2.0.floor #=> 2 (-1.2).floor #=> -2 (-2.0).floor #=> -2 1.234567.floor(2) #=> 1.23 1.234567.floor(3) #=> 1.234 1.234567.floor(4) #=> 1.2345 1.234567.floor(5) #=> 1.23456 34567.89.floor(-5) #=> 0 34567.89.floor(-4) #=> 30000 34567.89.floor(-3) #=> 34000 34567.89.floor(-2) #=> 34500 34567.89.floor(-1) #=> 34560 34567.89.floor(0) #=> 34567 34567.89.floor(1) #=> 34567.8 34567.89.floor(2) #=> 34567.89 34567.89.floor(3) #=> 34567.89
Note that the limited precision of floating point arithmetic might lead to surprising results:
(0.3 / 0.1).floor #=> 2 (!)
Returns the value as a rational.
2.0.to_r #=> (2/1) 2.5.to_r #=> (5/2) -0.75.to_r #=> (-3/4) 0.0.to_r #=> (0/1) 0.3.to_r #=> (5404319552844595/18014398509481984)
NOTE: 0.3.to_r isn’t the same as “0.3”.to_r. The latter is equivalent to “3/10”.to_r, but the former isn’t so.
0.3.to_r == 3/10r #=> false "0.3".to_r == 3/10r #=> true
See also Float#rationalize
.
Resumes the fiber from the point at which the last Fiber.yield
was called, or starts running it if it is the first call to resume
. Arguments passed to resume will be the value of the Fiber.yield
expression or will be passed as block parameters to the fiber’s block if this is the first resume
.
Alternatively, when resume is called it evaluates to the arguments passed to the next Fiber.yield
statement inside the fiber’s block or to the block value if it runs to completion without any Fiber.yield
Returns fiber information string.
Returns the current fiber. You need to require 'fiber'
before using this method. If you are not running in the context of a fiber this method will return the root fiber.
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"]
Reads the next entry from dir and returns it as a string. Returns nil
at the end of the stream.
d = Dir.new("testdir") d.read #=> "." d.read #=> ".." d.read #=> "config.h"