Been around a blocks few times


#1



https://www.codecademy.com/courses/ruby-beginner-en-L3ZCI/3/1?curriculum_id=5059f8619189a5000201fbcb#


It says 42 should be in ints but it isn't. What I'm missing ?


odds_n_ends = [:weezard, 42, "Trady Blix", 3, true, 19, 12.345]
ints = odds_n_ends.select {is_a? Integer}


#2

I'm bit of a tourist when it comes to Ruby, but I think that checks if the main object is an integer. It's not. See Array#select's documentation for an example:
http://ruby-doc.org/core-2.2.0/Array.html#method-i-select


#3

This is updated code:

odds_n_ends = [:weezard, 42, "Trady Blix", 3, true, 19, 12.345]
ints = odds_n_ends.select {|odds_n_ends| is_a? Integer}

but still getting the same error .... this should work though ..... syntax should be ok ...


#4

Your block names a parameter now, but ignores it
It's also questionable to name it the same as a variable that represents something very different.


#5

Consider what the block should do.

It receives one value, and should determine if it should be included.

Your block is doing something similar to this right now:

def add a, b
    42
end

puts add 5, 6  # outputs 42, but I expect 11

That method ignores the arguments given to it and just returns 42. It's a fairly useless method, it doesn't say anything about the sum of a and b.


#6

This is a bit of an old thread, but if by chance you still haven't solved it. I was having the same trouble and it finally came to me with ionatan's comment about ignoring the parameter.

if you have a block with a parameter then you also need to use that parameter in the block.
for example:
it would be times_2 = some bit of code { |x| x * 2 }
not times_2 = some bit of code { |x| * 2 }

Hope that helps to clear it up.


#8