Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - test for array

Tags:

syntax

ruby

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?

like image 516
BuddyJoe Avatar asked Oct 06 '09 20:10

BuddyJoe


People also ask

How do I check if an array is in Ruby?

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.

How do you check if an array is null in Ruby?

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 .

How do you check if two arrays are equal in Ruby?

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.

How do you check if an element is present in an array or not?

The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

What is an array in Ruby?

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.

What is a negative index in Ruby array?

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.

How to get multiple elements from array in Ruby?

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.

How to iterate over the elements of an array in Ruby?

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.


3 Answers

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
like image 104
ry. Avatar answered Oct 20 '22 06:10

ry.


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')
like image 158
zgchurch Avatar answered Oct 20 '22 07:10

zgchurch


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.

like image 61
DigitalRoss Avatar answered Oct 20 '22 06:10

DigitalRoss