Skip to content

Instantly share code, notes, and snippets.

@ben-biddington
Last active August 29, 2015 14:04
Show Gist options
  • Save ben-biddington/34d7326f916cd520bfb9 to your computer and use it in GitHub Desktop.
Save ben-biddington/34d7326f916cd520bfb9 to your computer and use it in GitHub Desktop.
An object/data structure anti-symmetry example

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?

# Start with a `database` represented as a data structure, with a `Publisher` that performs the task of installation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment