I'm just starting with Ruby and I hit an issues which I feel is important for the language so I don't want to just pass it. I would really appreciate answer which includes not only a working example but at least brief explanation where I went wrong with mine.
So first step is having this method:
def filter (arr)
arr.each do |e|
puts e if e % 2 != 0
end
end
filter [1, 2, 3, 4, 5, 6]
And the result as expected is:
1 3 5 [Finished in 0.2s]
Second I tried this one:
def filter (arr)
arr.each do |e|
puts e if yield(e)
end
end
filter ([1, 2, 3, 4, 5, 6]) { |n| n.odd? }
And I got the same result:
1 3 5 [Finished in 0.2s]
Third I want to do this using a lambda. Ultimately I want ti invoke the filter method like so filter([1, 2, 3, 4, 5, 6], &is_odd). But since I can't still figure it out I am currently stuck at this:
is_odd = lambda { |n| puts n if n.odd? }
def filter ()
arr = [1, 2, 3, 4, 5, 6]
arr.each do |e|
is_odd(e)
end
end
filter &is_odd
And I got the following error:
block in filter': undefined methodis_odd' for main:Object (NoMethodError)
It kind of makes sense to me that this is not working since, if I define the lambda inside the filter function and use it like so:
def filter ()
is_odd = lambda { |n| puts n if n.odd? }
arr = [1, 2, 3, 4, 5, 6]
arr.each &is_odd
end
filter
I'm again getting the expected behavior, but I am following a tutorial and it seems like it should be possible to declare is_odd outside the filter method and invoke filter like so filter([1, 2, 3, 4, 5, 6], &is_odd).
I would like to know if indeed it is possible to do use lambda this way and if yes, where did my logic fail?