Iterates over the range, passing each n
th element to the block. If begin and end are numeric, n
is added for each iteration. Otherwise step
invokes succ to iterate through range elements.
If no block is given, an enumerator is returned instead. Especially, the enumerator is an Enumerator::ArithmeticSequence
if begin and end of the range are numeric.
range = Xs.new(1)..Xs.new(10) range.step(2) {|x| puts x} puts range.step(3) {|x| puts x}
produces:
1 x 3 xxx 5 xxxxx 7 xxxxxxx 9 xxxxxxxxx 1 x 4 xxxx 7 xxxxxxx 10 xxxxxxxxxx
See Range
for the definition of class Xs.
Returns the first object in the range, or an array of the first n
elements.
(10..20).first #=> 10 (10..20).first(3) #=> [10, 11, 12]
Returns the last object in the range, or an array of the last n
elements.
Note that with no arguments last
will return the object that defines the end of the range even if exclude_end?
is true
.
(10..20).last #=> 20 (10...20).last #=> 20 (10..20).last(3) #=> [18, 19, 20] (10...20).last(3) #=> [17, 18, 19]
Returns an array containing the items in the range.
(1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7] (1..).to_a #=> RangeError: cannot convert endless range to an array
Convert this range object to a printable form (using to_s
to convert the begin and end objects).
Returns a string containing the regular expression and its options (using the (?opts:source)
notation. This string can be fed back in to Regexp::new
to a regular expression with the same semantics as the original. (However, Regexp#==
may not return true when comparing the two, as the source of the regular expression itself may differ, as the example shows). Regexp#inspect
produces a generally more readable version of rxp.
r1 = /ab+c/ix #=> /ab+c/ix s1 = r1.to_s #=> "(?ix-m:ab+c)" r2 = Regexp.new(s1) #=> /(?ix-m:ab+c)/ r1 == r2 #=> false r1.source #=> "ab+c" r2.source #=> "(?ix-m:ab+c)"
Replaces the contents of the set with the contents of the given enumerable object and returns self.
set = Set[1, 'c', :s] #=> #<Set: {1, "c", :s}> set.replace([1, 2]) #=> #<Set: {1, 2}> set #=> #<Set: {1, 2}>
Converts the set to an array. The order of elements is uncertain.
Set[1, 2].to_a #=> [1, 2] Set[1, 'c', :s].to_a #=> [1, "c", :s]
Equivalent to Set#delete_if
, but returns nil if no changes were made. Returns an enumerator if no block is given.
Resets the internal state after modification to existing elements and returns self.
Elements will be reindexed and deduplicated.
Returns the name or string corresponding to sym.
:fred.id2name #=> "fred" :ginger.to_s #=> "ginger"
Note that this string is not frozen (unlike the symbol itself). To get a frozen string, use name
.
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
Returns the children of the directory (files and subdirectories, not recursive) as an array of Pathname
objects.
By default, the returned pathnames will have enough information to access the files. If you set with_directory
to false
, then the returned pathnames will contain the filename only.
For example:
pn = Pathname("/usr/lib/ruby/1.8") pn.children # -> [ Pathname:/usr/lib/ruby/1.8/English.rb, Pathname:/usr/lib/ruby/1.8/Env.rb, Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ] pn.children(false) # -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
Note that the results never contain the entries .
and ..
in the directory because they are not children.
Recursively deletes a directory, including all directories beneath it.
See FileUtils.rm_r
Return the path as a String
.
to_path
is implemented so Pathname
objects are usable with File.open
, etc.
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.
Returns all the bytes from the file, or the first N
if specified.
See File.binread
.