Last active
December 17, 2015 16:19
-
-
Save brianknapp/5637790 to your computer and use it in GitHub Desktop.
Making your code more obvious with descriptive message passing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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