Returns true
if the given object
is present in self
(that is, if any element ==
object
), otherwise returns false
.
a = [ "a", "b", "c" ] a.include?("b") #=> true a.include?("z") #=> false
Returns a new array by removing duplicate values in self
.
If a block is given, it will use the return value of the block for comparison.
It compares values using their hash
and eql?
methods for efficiency.
self
is traversed in order, and the first occurrence is kept.
a = [ "a", "a", "b", "b", "c" ] a.uniq # => ["a", "b", "c"] b = [["student","sam"], ["student","george"], ["teacher","matz"]] b.uniq { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
Removes duplicate elements from self
.
If a block is given, it will use the return value of the block for comparison.
It compares values using their hash
and eql?
methods for efficiency.
self
is traversed in order, and the first occurrence is kept.
Returns nil
if no changes are made (that is, no duplicates are found).
a = [ "a", "a", "b", "b", "c" ] a.uniq! # => ["a", "b", "c"] b = [ "a", "b", "c" ] b.uniq! # => nil c = [["student","sam"], ["student","george"], ["teacher","matz"]] c.uniq! { |s| s.first } # => [["student", "sam"], ["teacher", "matz"]]
Returns a copy of self
with all nil
elements removed.
[ "a", nil, "b", nil, "c", nil ].compact #=> [ "a", "b", "c" ]
Removes nil
elements from the array.
Returns nil
if no changes were made, otherwise returns the array.
[ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ] [ "a", "b", "c" ].compact! #=> nil
Returns the number of elements.
If an argument is given, counts the number of elements which equal obj
using ==
.
If a block is given, counts the number of elements for which the block returns a true value.
ary = [1, 2, 4, 2] ary.count #=> 4 ary.count(2) #=> 2 ary.count { |x| x%2 == 0 } #=> 3
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object+ types
mathn serves to make mathematical operations more precise in Ruby and to integrate other mathematical standard libraries.
Without mathn:
3 / 2 => 1 # Integer
With mathn:
3 / 2 => 3/2 # Rational
mathn keeps value in exact terms.
Without mathn:
20 / 9 * 3 * 14 / 7 * 3 / 2 # => 18
With mathn:
20 / 9 * 3 * 14 / 7 * 3 / 2 # => 20
When you require ‘mathn’, the libraries for Prime
, CMath
, Matrix
and Vector
are also loaded.
Author: Keiju ISHITSUKA (SHL Japan Inc.)
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object
types
Produces a shallow copy of obj—the instance variables of obj are copied, but not the objects they reference. clone
copies the frozen (unless :freeze keyword argument is given with a false value) and tainted state of obj. See also the discussion under Object#dup
.
class Klass attr_accessor :str end s1 = Klass.new #=> #<Klass:0x401b3a38> s1.str = "Hello" #=> "Hello" s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello"> s2.str[1,4] = "i" #=> "i" s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">" s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">"
This method may have class-specific behavior. If so, that behavior will be documented under the #initialize_copy
method of the class.
Deprecated method that is equivalent to taint
.
Deprecated method that is equivalent to tainted?
.
Returns a string containing a human-readable representation of obj. The default inspect
shows the object’s class name, an encoding of the object id, and a list of the instance variables and their values (by calling inspect
on each of them). User defined classes should override this method to provide a better representation of obj. When overriding this method, it should return a string whose encoding is compatible with the default external encoding.
[ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]" Time.new.inspect #=> "2008-03-08 19:43:39 +0900" class Foo end Foo.new.inspect #=> "#<Foo:0x0300c868>" class Bar def initialize @bar = 1 end end Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
provides a unified clone
operation, for REXML::XPathParser
to use across multiple Object
types
Iterates the given block int
times, passing in values from zero to int - 1
.
If no block is given, an Enumerator
is returned instead.
5.times do |i| print i, " " end #=> 0 1 2 3 4
Rounds int
to a given precision in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits
is positive, self
for zero, and round down for negative.
1.round #=> 1 1.round(2) #=> 1.0 15.round(-1) #=> 20
Returns a string containing the representation of int
radix base
(between 2 and 36).
12345.to_s #=> "12345" 12345.to_s(2) #=> "11000000111001" 12345.to_s(8) #=> "30071" 12345.to_s(10) #=> "12345" 12345.to_s(16) #=> "3039" 12345.to_s(36) #=> "9ix" 78546939656932.to_s(36) #=> "rubyrules"
Returns a complex object which denotes the given rectangular form.
Complex.rectangular(1, 2) #=> (1+2i)
Returns a complex object which denotes the given rectangular form.
Complex.rectangular(1, 2) #=> (1+2i)
Returns the value as a string for inspection.
Complex(2).inspect #=> "(2+0i)" Complex('-8/6').inspect #=> "((-4/3)+0i)" Complex('1/2i').inspect #=> "(0+(1/2)*i)" Complex(0, Float::INFINITY).inspect #=> "(0+Infinity*i)" Complex(Float::NAN, Float::NAN).inspect #=> "(NaN+NaN*i)"
Always returns the string “nil”.