Skip to content

Commit

Permalink
Sample Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Cheail committed Apr 13, 2011
0 parents commit cbebc7b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sample code for the RubySource article [Making Ruby Quack—Why We Love Duck Typing](http://rubysource.com)
35 changes: 35 additions & 0 deletions example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class ShippingManifest
attr_accessor :mass, :package_count

def initialize(mass, package_count)
@mass = mass
@package_count = package_count
end

def +(other)
self.dup.tap do |neu|
neu.mass = self.mass + other.mass
neu.package_count = self.package_count + other.package_count
end
end
end


# Addition:
shipping_manifest_one = ShippingManifest.new(5, 10)
shipping_manifest_two = ShippingManifest.new(1, 1)
shipping_manifest_sum = shipping_manifest_one + shipping_manifest_two

shipping_manifest_sum.package_count # => 11
shipping_manifest_sum.mass # => 6

puts "shipping_manifest_sum: #{shipping_manifest_sum.inspect}"

# Injection:
manifests = [
ShippingManifest.new(5, 10),
ShippingManifest.new(1, 1),
ShippingManifest.new(35, 50),
]

manifests.inject { |sum, m| sum + m } # => #<ShippingManifest:0x100164c38 @package_count=61, @mass=41>

0 comments on commit cbebc7b

Please sign in to comment.