Skip to content

Instantly share code, notes, and snippets.

@brianknapp
Last active December 17, 2015 16:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianknapp/5637790 to your computer and use it in GitHub Desktop.
Save brianknapp/5637790 to your computer and use it in GitHub Desktop.
Making your code more obvious with descriptive message passing
require 'objective-ruby'
class Sandwich
end
me = Object.new
cheese_sandwich = Sandwich.new
puts "With Objective Ruby:"
class ObjectiveSandwichArtist
include ObjectiveRuby
objc :make, the_customer: [:customer, Object], a: [:sandwich, Sandwich], with_toppings: [:toppings, Array] do |message|
puts "Message: #{message.inspect}"
end
end
sandwich_artist = ObjectiveSandwichArtist.new
sandwich_artist.make the_customer: me, a: cheese_sandwich, with_toppings: ["Mayo", "Butter", "Lettuce"]
puts "With Ruby:"
class SandwichArtist
def make customer, specifications
puts "Customer: #{customer}"
puts "Specifications: #{specifications.inspect}"
end
end
sandwich_artist = SandwichArtist.new
sandwich_artist.make me, a: cheese_sandwich, with_toppings: ["Mayo", "Butter", "Lettuce"]
puts "With Future Obvious:"
class ObviousSandwichArtist
include ObviousObject
define :make, the_customer: [:customer, Object], a: [:sandwich, Sandwich], with_toppings: [:toppings, Array] do |message|
puts "Message: #{message.inspect}"
end
end
sandwich_artist = ObviousSandwichArtist.new
sandwich_artist.make the_customer: me, a: cheese_sandwich, with_toppings: ["Mayo", "Butter", "Lettuce"]
# Output:
#
# With Objective Ruby:
# Message: {:customer=>#<Object:0x007fb942948128>, :sandwich=>#<Sandwich:0x007fb942948088>, :toppings=>["Mayo", "Butter", "Lettuce"]}
# With Ruby:
# Customer: #<Object:0x007fb942948128>
# Specifications: {:a=>#<Sandwich:0x007fb942948088>, :with_toppings=>["Mayo", "Butter", "Lettuce"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment