Ruby, etc

Learn all the things

He who enjoys doing and enjoys what he has done is happy. - Fortune Cookie

Enumerable: Any?

This is about the handy any? method in Enumerable

The documentation says

Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil. If the block is not given, Ruby adds an implicit block of { |obj| obj } that will cause any? to return true if at least one of the collection members is not false or nil.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Taco
  attr_accessor :meat

  def initialize(meat)
    @meat = meat
  end

  def beef?
    @meat == :beef
  end

  def chicken?
    @meat == :chicken
  end

end

Lets test it out

1
2
3
4
5
6
7
8
irb(main):018:0> taco = Taco.new(:beef)
=> #<Taco:0x007fa374070278 @meat=:beef>

irb(main)> taco.beef?
=> true

irb(main)> taco.chicken?
=> false

Lets create two instances

1
2
3
tacos = []
tacos.push(Taco.new(:beef))
tacos.push(Taco.new(:chicken))

Now since the array has enumerable and we made some handy methods to test the meat of our tacos we can easily check our array to see if we have any chicken tacos:

1
tacos.any? { |taco| taco.chicken? }

If all the elements of the array are booleans as in this case we don’t need the block version:

1
2
3
4
5
irb(main)> possible_chicken_tacos = [tacos[0].chicken?, tacos[1].chicken?]
=> [true, false]

irb(main)> possible_chicken_tacos.any?
=> true

Yep, today was definately a chicken taco day. :)

Comments