What is the right way to:
is_array("something") # => false (or 1)
is_array(["something", "else"]) # => true (or > 1)
or to get the count of items in it?
This is another way to do this: use the Array#index method. It returns the index of the first occurrence of the element in the array. This returns the index of the first word in the array that contains the letter 'o'. index still iterates over the array, it just returns the value of the element.
To check if a array is empty or not, we can use the built-in empty? method in Ruby. The empty? method returns true if a array is empty; otherwise, it returns false .
Arrays can be equal if they have the same number of elements and if each element is equal to the corresponding element in the array. To compare arrays in order to find if they are equal or not, we have to use the == operator.
The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.
Ruby arrays are ordered, integer-indexed collections of any object. Each element in an array is associated with and referred to by an index. Array indexing starts at 0, as in C or Java.
Ruby - Arrays. A negative index is assumed relative to the end of the array --- that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on. Ruby arrays can hold objects such as String, Integer, Fixnum, Hash, Symbol, even other Array objects.
Ruby arrays provide a lot of different methods to access the array element. But the most used way is to use the index of an array. Retrieving Multiple Elements from Array: There can be many situations where the user need to access the multiple elements from the array.
In Ruby the C-like for-loop is not in use. Instead of that people usually iterate over the elements of an array using the each method. examples/ruby/iterating_on_array.rb. names = ['Foo', 'Bar', 'Baz'] puts names puts names.each { |item| puts item } puts names.each do |item| puts item end. In this example we have an array with 3 elements.
You probably want to use kind_of()
.
>> s = "something"
=> "something"
>> s.kind_of?(Array)
=> false
>> s = ["something", "else"]
=> ["something", "else"]
>> s.kind_of?(Array)
=> true
Are you sure it needs to be an array? You may be able to use respond_to?(method)
so your code would work for similar things that aren't necessarily arrays (maybe some other enumberable thing). If you do actually need an array
, then the post describing the Array#kind\_of?
method is best.
['hello'].respond_to?('each')
Instead of testing for an Array,
just convert whatever you get into a one-level Array,
so your code only needs to handle the one case.
t = [*something] # or...
t = Array(something) # or...
def f *x
...
end
Ruby has various ways to harmonize an API which can take an object or an Array of objects, so, taking a guess at why you want to know if something is an Array, I have a suggestion.
The splat operator contains lots of magic you can look up, or you can just call Array(something)
which will add an Array wrapper if needed. It's similar to [*something]
in this one case.
def f x
p Array(x).inspect
p [*x].inspect
end
f 1 # => "[1]"
f [1] # => "[1]"
f [1,2] # => "[1, 2]"
Or, you could use the splat in the parameter declaration and then .flatten
, giving you a different sort of collector. (For that matter, you could call .flatten
above, too.)
def f *x
p x.flatten.inspect
end # => nil
f 1 # => "[1]"
f 1,2 # => "[1, 2]"
f [1] # => "[1]"
f [1,2] # => "[1, 2]"
f [1,2],3,4 # => "[1, 2, 3, 4]"
And, thanks gregschlom, it's sometimes faster to just use Array(x)
because when it's already an Array
it doesn't need to create a new object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With