Ruby - Test for Array

Ruby - test for array

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

How to check if a value exists in an array in Ruby

You're looking for include?:

>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true

What is an elegant way in Ruby to tell if a variable is a Hash or an Array?

You can just do:

@some_var.class == Hash

or also something like:

@some_var.is_a?(Hash)

It's worth noting that the "is_a?" method is true if the class is anywhere in the objects ancestry tree. for instance:

@some_var.is_a?(Object)  # => true

the above is true if @some_var is an instance of a hash or other class that stems from Object. So, if you want a strict match on the class type, using the == or instance_of? method is probably what you're looking for.

Ruby: check if all array elements are equal

You could also use .uniq, that returns an array with no duplicates, and check the size:

def all_equal?(arr)
arr.uniq.size <= 1
end

Ruby String variable returning true for .is_a?(Array)

def do_stuff(x)
x = x.lines.to_a if x.is_a? String
x
end

data = [
"hello\nworld",
[1, 2, 3]
]

data.each do |item|
p do_stuff item
end

Now, with unless:

def do_stuff(x)
unless x.is_a?(Array)
x = x.lines.to_a
end
x
end

data = [
"hello\nworld",
[1, 2, 3],
['a', 'b']
]

data.each do |item|
p do_stuff item
end

--output:--
["hello\n", "world"]
[1, 2, 3]
["a", "b"]

But it makes more sense to check for a String object before calling a String method on the object than checking for not an Array.

How do you check an array for a range in Ruby?

Edit 2: This is my absolutely final solution:

require 'set'
STRAIGHTS = ['A',*2..9,'T','J','Q','K','A'].each_cons(5).map(&:to_set)
#=> [#<Set: {"A", 2, 3, 4, 5}>, #<Set: {2, 3, 4, 5, 6}>,
# ...#<Set: {9, "T", "J", "Q", "K"}>, #<Set: {"T", "J", "Q", "K", "A"}>]

def straight?(hand)
STRAIGHTS.include?(hand.to_set)
end

STRAIGHTS.include?([6,3,4,5,2].to_set)
# STRAIGHTS.include?(#<Set: {6, 3, 4, 5, 2}>)
#=> true

straight?([6,5,4,3,2]) #=> true
straight?(["T","J","Q","K","A"]) #=> true
straight?(["A","K","Q","J","T"]) #=> true
straight?([2,3,4,5,"A"]) #=> true

straight?([6,7,8,9,"J"]) #=> false
straight?(["J",7,8,9,"T"]) #=> false

Edit 1: @mudasobwa upset the apple cart by pointing out that 'A',2,3,4,5 is a valid straight. I believe I've fixed my answer. (I trust he's not going to tell me that 'K','A',2,3,4 is also valid.)

I would suggest the following:

CARDS     = [2, 3, 4, 5, 6, 7, 8, 9, "T", "J", "Q", "K", "A"]
STRAIGHTS = CARDS.each_cons(5).to_a
#=>[[2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8],
# [5, 6, 7, 8, 9], [6, 7, 8, 9, "T"], [7, 8, 9, "T", "J"],
# [8, 9, "T", "J", "Q"], [9, "T", "J", "Q", "K"],
# ["T", "J", "Q", "K", "A"]]

def straight?(hand)
(hand.map {|c| CARDS.index(c)}.sort == [0,1,2,3,12]) ||
STRAIGHTS.include?(hand.sort {|a,b| CARDS.index(a) <=> CARDS.index(b)})
end

How to check if a key exists in an array of arrays?

If you only need a true/false answer you can flatten the array and call include on that:

>> myArray.flatten.include?("c")
=> true

Ruby - test each array element, get one result

array.all?{ |x| x.is_a? Integer }


Related Topics



Leave a reply



Submit