The following is a functional solution to the Bob
exercism example:
class Bob
def hey(what)
return 'Woah, chill out!' if shouted?(what)
return 'Sure.' if question?(what)
return 'Fine. Be that way!' if blank?(what)
'Whatever.'
end
private
def shouted?(what)
!blank?(what) && what.upcase === what
end
def question?(what); what.end_with? '?'; end
def blank?(what); (what || '').strip.empty?; end
end
The what
parameter is treated as a value object. Functions are used to achieve the behaviour.
Q. How can this be vulnerable to new types? Q. Why is this a reasonable solution?