Returns a new array with elements of self
shuffled.
a = [ 1, 2, 3 ] #=> [1, 2, 3] a.shuffle #=> [2, 3, 1] a #=> [1, 2, 3]
The optional rng
argument will be used as the random number generator.
a.shuffle(random: Random.new(1)) #=> [1, 3, 2]
Choose a random element or n
random elements from the array.
The elements are chosen by using random and unique indices into the array in order to ensure that an element doesn’t repeat itself unless the array already contained duplicate elements.
If the array is empty the first form returns nil
and the second form returns an empty array.
The optional rng
argument will be used as the random number generator.
a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] a.sample #=> 7 a.sample(4) #=> [6, 4, 2, 5]
Calls the given block for each element n
times or forever if nil
is given.
Does nothing if a non-positive number is given or the array is empty.
Returns nil
if the loop has finished without getting interrupted.
If no block is given, an Enumerator
is returned instead.
a = ["a", "b", "c"] a.cycle { |x| puts x } # print, a, b, c, a, b, c,.. forever. a.cycle(2) { |x| puts x } # print, a, b, c, a, b, c.
Returns true
if big is an odd number.
Returns the angle part of its polar form.
Complex.polar(3, Math::PI/2).arg #=> 1.5707963267948966
Returns 0 if the value is positive, pi otherwise.
Convert self
to locale encoding
Escapes str
so that it can be safely used in a Bourne shell command line.
See Shellwords.shellescape
for details.
Returns the character length of str.
Produces a version of str
with all non-printing characters replaced by \nnn
notation and all special characters escaped.
"hello \n ''".dump #=> "\"hello \\n ''\"
Returns an array of the Integer
ordinals of the characters in str. This is a shorthand for str.each_codepoint.to_a
.
If a block is given, which is a deprecated form, works the same as each_codepoint
.
Returns a copy of str with all characters in the intersection of its arguments deleted. Uses the same rules for building the set of characters as String#count
.
"hello".delete "l","lo" #=> "heo" "hello".delete "lo" #=> "he" "hello".delete "aeiou", "^e" #=> "hell" "hello".delete "ej-m" #=> "ho"
Performs a delete
operation in place, returning str, or nil
if str was not modified.
Returns 0 if the value is positive, pi otherwise.
Returns the file descriptor used in dir.
d = Dir.new("..") d.fileno #=> 8
This method uses dirfd() function defined by POSIX 2008. NotImplementedError
is raised on other platforms, such as Windows, which doesn’t provide the function.
Deletes the named directory. Raises a subclass of SystemCallError
if the directory isn’t empty.
Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. See also Dir::rmdir
.
Returns true
if the named file is readable by the effective user and group id of this process. See eaccess(3).
Returns true
if the named file is writable by the effective user and group id of this process. See eaccess(3).
Returns true
if the named file is executable by the effective user and group id of this process. See eaccess(3).
Returns true
if the named file
exists and is a regular file.
file
can be an IO
object.
If the file
argument is a symbolic link, it will resolve the symbolic link and use the file referenced by the link.
Returns true for dummy encodings. A dummy encoding is an encoding for which character handling is not properly implemented. It is used for stateful encodings.
Encoding::ISO_2022_JP.dummy? #=> true Encoding::UTF_8.dummy? #=> false
Returns the return value of the iterator.
o = Object.new def o.each yield 1 yield 2 yield 3 100 end e = o.to_enum puts e.next #=> 1 puts e.next #=> 2 puts e.next #=> 3 begin e.next rescue StopIteration => ex puts ex.result #=> 100 end
The primary interface to this library. Use to setup delegation when defining your class.
class MyClass < DelegateClass(ClassToDelegateTo) # Step 1 def initialize super(obj_of_ClassToDelegateTo) # Step 2 end end
Here’s a sample of use from Tempfile
which is really a File
object with a few special rules about storage location and when the File
should be deleted. That makes for an almost textbook perfect example of how to use delegation.
class Tempfile < DelegateClass(File) # constant and class member data initialization... def initialize(basename, tmpdir=Dir::tmpdir) # build up file path/name in var tmpname... @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600) # ... super(@tmpfile) # below this point, all methods of File are supported... end # ... end