Let me preface this by saying I am very new to Ruby. I am going through couple exercises writing my own versions of some enumerable functions. Everything was working fine until it kinda just stopped working. I don't think I changed anything. I have function that looks as follows:
def my_all?
return false unless block_given?
for i in self
if !yield(i)
return false
end
end
return true
end
and I call it as follows:
arr = [1,2,3,4]
print arr.my_all? do |num|
num == num
end
This should return true because every num is obviously equal to itself. However I get a false return value. After some mucking around I found out that ruby is not detecting the do...end block passed to the my_all? method (block_given? is returning false). The method DOES work if I change the do..end block to a single line bracket block as follows:
{ |num| num == num}
Can somebody please tell me what I am doing wrong regarding the do..end version? Thanks