Returns the integer ordinal of the first character of self
:
'h'.ord # => 104 'hello'.ord # => 104 'тест'.ord # => 1090 'こんにちは'.ord # => 12371
Returns a left-justified copy of self
.
If integer argument size
is greater than the size (in characters) of self
, returns a new string of length size
that is a copy of self
, left justified and padded on the right with pad_string
:
'hello'.ljust(10) # => "hello " ' hello'.ljust(10) # => " hello " 'hello'.ljust(10, 'ab') # => "helloababa" 'тест'.ljust(10) # => "тест " 'こんにちは'.ljust(10) # => "こんにちは "
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.ljust(5) # => "hello" 'hello'.ljust(1) # => "hello"
Related: String#rjust
, String#center
.
Returns a right-justified copy of self
.
If integer argument size
is greater than the size (in characters) of self
, returns a new string of length size
that is a copy of self
, right justified and padded on the left with pad_string
:
'hello'.rjust(10) # => " hello" 'hello '.rjust(10) # => " hello " 'hello'.rjust(10, 'ab') # => "ababahello" 'тест'.rjust(10) # => " тест" 'こんにちは'.rjust(10) # => " こんにちは"
If size
is not greater than the size of self
, returns a copy of self
:
'hello'.rjust(5, 'ab') # => "hello" 'hello'.rjust(1, 'ab') # => "hello"
Related: String#ljust
, String#center
.
Returns a copy of the receiver with leading and trailing whitespace removed; see Whitespace in Strings:
whitespace = "\x00\t\n\v\f\r " s = whitespace + 'abc' + whitespace s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r " s.strip # => "abc"
Related: String#lstrip
, String#rstrip
.
Returns a copy of self
with leading whitespace removed; see Whitespace in Strings:
whitespace = "\x00\t\n\v\f\r " s = whitespace + 'abc' + whitespace s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r " s.lstrip # => "abc\u0000\t\n\v\f\r "
Related: String#rstrip
, String#strip
.
Returns a copy of the receiver with trailing whitespace removed; see Whitespace in Strings:
whitespace = "\x00\t\n\v\f\r " s = whitespace + 'abc' + whitespace s # => "\u0000\t\n\v\f\r abc\u0000\t\n\v\f\r " s.rstrip # => "\u0000\t\n\v\f\r abc"
Related: String#lstrip
, String#strip
.
Like String#strip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#lstrip!
, String#strip!
.
Like String#lstrip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#rstrip!
, String#strip!
.
Like String#rstrip
, except that any modifications are made in self
; returns self
if any modification are made, nil
otherwise.
Related: String#lstrip!
, String#strip!
.
Returns the value of float
as a BigDecimal
. The precision
parameter is used to determine the number of significant digits for the result. When precision
is set to 0
, the number of digits to represent the float being converted is determined automatically. The default precision
is 0
.
require 'bigdecimal' require 'bigdecimal/util' 0.5.to_d # => 0.5e0 1.234.to_d # => 0.1234e1 1.234.to_d(2) # => 0.12e1
See also Kernel.BigDecimal
.
Returns a string containing a representation of self
; depending of the value of self
, the string representation may contain:
A fixed-point number.
A number in “scientific notation” (containing an exponent).
‘Infinity’.
‘-Infinity’.
‘NaN’ (indicating not-a-number).
3.14.to_s # => “3.14” (10.1**50).to_s # => “1.644631821843879e+50” (10.1**500).to_s # => “Infinity” (-10.1**500).to_s # => “-Infinity” (0.0/0.0).to_s # => “NaN”
Returns self
truncated to an Integer
.
1.2.to_i # => 1 (-1.2).to_i # => -1
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).to_i # => 2 (!)
Returns the largest number less than or equal to self
with 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.floor(1) # => 12345.6 f.floor(3) # => 12345.678 f = -12345.6789 f.floor(1) # => -12345.7 f.floor(3) # => -12345.679
When ndigits
is non-positive, returns an integer with at least ndigits.abs
trailing zeros:
f = 12345.6789 f.floor(0) # => 12345 f.floor(-3) # => 12000 f = -12345.6789 f.floor(0) # => -12346 f.floor(-3) # => -13000
Note that the limited precision of floating-point arithmetic may lead to surprising results:
(0.3 / 0.1).floor #=> 2 (!)
Related: Float#ceil
.
Returns self
(which is already a Float).
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
.
Returns the current fiber. If you are not running in the context of a fiber this method will return the root fiber.
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 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.
Reads and returns the next entry name from self
; returns nil
if at end-of-stream; see Dir As Stream-Like:
dir = Dir.new('example') dir.read # => "." dir.read # => ".." dir.read # => "config.h"
Returns an array of the entry names in self
except for '.'
and '..'
:
dir = Dir.new('/example') dir.children # => ["config.h", "lib", "main.rb"]
Sets the position in self
to zero; see Dir As Stream-Like:
dir = Dir.new('example') dir.read # => "." dir.read # => ".." dir.pos # => 2 dir.rewind # => #<Dir:example> dir.pos # => 0
Returns whether dirpath
is a directory in the underlying file system:
Dir.exist?('/example') # => true Dir.exist?('/nosuch') # => false Dir.exist?('/example/main.rb') # => false
Same as File.directory?
.
Returns a File::Stat
object for the file at filepath
(see File::Stat
):
File.stat('t.txt').class # => File::Stat
Like File::stat
, but does not follow the last symbolic link; instead, returns a File::Stat
object for the link itself.
File.symlink('t.txt', 'symlink') File.stat('symlink').size # => 47 File.lstat('symlink').size # => 5