Regarding if statement scope in Ruby
Aug 31, 2013I recently learned that if
statements in Ruby do not introduce scope. This means that you can write code like shown below and it’ll work fine.
# perfectly valid Ruby code
if true
foo = 5
end
puts foo
At first this seemed a bit weird to me. It wasn’t until I read this that I realized Ruby was even more versatile than I had first thought. As it turns out, it is this somewhat unconventional scoping rule that allows us to conditionally replace methods.
if foo == 5
def some_method
# do something
end
else
def some_method
# do something else
end
end
As well as conditionally modify implementations.
if foo == 5
class someClass
# ...
end
else
module someModule
# ...
end
end
And that’s amazing!