In venturing into Ruby, I started toying with things like the way Ruby returns the last thing you've mentioned even if it was not after a return
construct. However, why don't these two snippets work the same way? Shouldn't they?
module Enumerable
def palindrome?
reversed_self = self.reverse
self.each_with_index {|el,index|
unless self[index]==reversed_self[index]
return false ## <-----
end
}
true
end
end
all good so far: puts ['foo','bar','baz'].palindrome? prints 'false'
and
module Enumerable
def palindrome?
reversed_self = self.reverse
self.each_with_index {|el,index|
unless self[index]==reversed_self[index]
false ## <------
end
}
true
end
end
puts ['foo','bar','baz'].palindrome? prints 'true' for some reason
What's the science behind this?