If you are a programmer then most probably you know that when the IF statement is used, it stands for options as far as the program flow is concerned. It is not different in Ruby on Rails and other programming languages since normally, the need to provide users of the program with the variable options is a need.
Here is a sample of the IF statement in code:
class BookList
def [](key)
if key.kind_of?(integer)
result = @Books[key]
else
result = @Books.find { |aBooks| key == sBooks.name}
end
return result
end
end
Further shortening the code we have above by using the ‘if’ statement as a modifier it becomes a shorter version of its former self as:
class Booklist
def [](key)
return @Books[key] if key.kind_of?(Integer)
return @Books.find { |aBooks| aBooks.name == key }
end
end
(Source) Artueel Blog
