cheshirecatalyst
cheshirecatalyst

Reputation: 379

rubymonk "all elements in an array are Fixnum"?

http://rubymonk.com/learning/books/1/problems/148-array_of_fixnum

Ruby monk suggests:

def array_of_fixnums?(array)
  array.all? { |x| x.is_a? Fixnum }
end

That is fine and all, however the following code works in irb 1.9.2 but fails when rubymonk passes an empty array:

def array_of_fixnums?(array)
  result = false
  array.each { |n|
    if n.is_a? Fixnum
      result = true
    else
      result = false
    end }
  result
end

here is the irb output:

1.9.2-p320 :001 > array_of_fixnums? []
 => false

and here is what rubymonk says about my solution:

returns 'true' for [1,2,3] ✔
returns 'false' for ['a',1,:b] ✔
returns 'true' for []
    RSpec::Expectations::ExpectationNotMetError
    expected false to be true

I'm wondering why this is so?

Update based on answers:

def array_of_fixnums?(array)
  result = true
  array.each { |n| return false unless n.is_a? Fixnum }
  result
end

Upvotes: 1

Views: 422

Answers (2)

Chuck
Chuck

Reputation: 237010

Your code has two problems:

  1. The problem is phrased in a slightly vague manner. What they actually want is for you to return false if any of the elements are not Fixnums, and true otherwise — so an empty array should give true. If you look at your code, you'll see that result starts out false, so if the array is empty, it will return false even though the test thinks it should be true. You can solve this by starting out with true.

  2. Your code actually just detects whether the last element of the array is a Fixnum. Let's take the array [1, "nope", 3]. It will first see 1 and set result to true, then it will see "nope" and set result to false, then it will see 3 and set result to true, and that's what the method will return. RubyMonks tests actually don't detect this error, but it would show up in the real world. You want to return immediately after getting a false result, as that is enough to determine that the array is not all Fixnums.

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

For this case you will never enter the each cycle as there are no elements in array. So you return the defualt value of result that you set to false on the line above. However if there are no elements in an array, then all of its elements are Fixnums and so you should return true.

Upvotes: 0

Related Questions