Returns the path associated with the IO
, or nil
if there is no path associated with the IO
. It is not guaranteed that the path exists on the filesystem.
$stdin.path # => "<STDIN>" File.open("testfile") {|f| f.path} # => "testfile"
Returns the numerator.
Rational(7).numerator #=> 7 Rational(7, 1).numerator #=> 7 Rational(9, -4).numerator #=> -9 Rational(-2, -10).numerator #=> 1
Returns the denominator (always positive).
Rational(7).denominator #=> 1 Rational(7, 1).denominator #=> 1 Rational(9, -4).denominator #=> 4 Rational(-2, -10).denominator #=> 5
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
Returns a simpler approximation of the value if the optional argument eps
is given (rat-|eps| <= result <= rat+|eps|), self otherwise.
r = Rational(5033165, 16777216) r.rationalize #=> (5033165/16777216) r.rationalize(Rational('0.01')) #=> (3/10) r.rationalize(Rational('0.1')) #=> (1/3)
With no block given, returns the MatchData
object that describes the match, if any, or nil
if none; the search begins at the given character offset
in string
:
/abra/.match('abracadabra') # => #<MatchData "abra"> /abra/.match('abracadabra', 4) # => #<MatchData "abra"> /abra/.match('abracadabra', 8) # => nil /abra/.match('abracadabra', 800) # => nil string = "\u{5d0 5d1 5e8 5d0}cadabra" /abra/.match(string, 7) #=> #<MatchData "abra"> /abra/.match(string, 8) #=> nil /abra/.match(string.b, 8) #=> #<MatchData "abra">
With a block given, calls the block if and only if a match is found; returns the block’s value:
/abra/.match('abracadabra') {|matchdata| p matchdata } # => #<MatchData "abra"> /abra/.match('abracadabra', 4) {|matchdata| p matchdata } # => #<MatchData "abra"> /abra/.match('abracadabra', 8) {|matchdata| p matchdata } # => nil /abra/.match('abracadabra', 8) {|marchdata| fail 'Cannot happen' } # => nil
Output (from the first two blocks above):
#<MatchData "abra"> #<MatchData "abra"> /(.)(.)(.)/.match("abc")[2] # => "b" /(.)(.)/.match("abc", 1)[2] # => "c"
Returns true
or false
to indicate whether the regexp is matched or not without updating $~ and other related variables. If the second parameter is present, it specifies the position in the string to begin the search.
/R.../.match?("Ruby") # => true /R.../.match?("Ruby", 1) # => false /P.../.match?("Ruby") # => false $& # => nil
Returns a new set that is a copy of the set, flattening each containing set recursively.
Equivalent to Set#flatten
, but replaces the receiver with the result in place. Returns nil if no modifications were made.
Equivalent to self.to_s.match
, including possible updates to global variables; see String#match
.
Equivalent to sym.to_s.match?
; see String#match
.
Allocates space for a new object of class’s class and does not call initialize on the new instance. The returned object must be an instance of class.
klass = Class.new do def initialize(*args) @initialized = true end def initialized? @initialized || false end end klass.allocate.initialized? #=> false
Returns clean pathname of self
with consecutive slashes and useless dots removed. The filesystem is not accessed.
If consider_symlink
is true
, then a more conservative algorithm is used to avoid breaking symbolic linkages. This may retain more ..
entries than absolutely necessary, but without accessing the filesystem, this can’t be avoided.
See Pathname#realpath
.
The opposite of Pathname#absolute?
It returns false
if the pathname begins with a slash.
p = Pathname.new('/im/sure') p.relative? #=> false p = Pathname.new('not/so/sure') p.relative? #=> true
Creates a full path, including any intermediate directories that don’t yet exist.
See FileUtils.mkpath
and FileUtils.mkdir_p
Returns the real (absolute) pathname for self
in the actual filesystem.
Does not contain symlinks or useless dots, ..
and .
.
All components of the pathname must exist when this method is called.
Returns the real (absolute) pathname of self
in the actual filesystem.
Does not contain symlinks or useless dots, ..
and .
.
The last component of the real pathname can be nonexistent.