Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
##hom_examples.rb # # Place ho_enumerable.rb and this file are in the same directory and # run the examples by : # # ruby hom_examples.rb # or # irb -r hom_examples.rb require File.join(__FILE__, '..', 'ho_enumerable' ) STUFF = [[ 'Mo', 'bowly', 'eye poking' ], [ 'Larry', 'curly', 'whomping' ], [ 'Curly', nil, 'whining' ]] class Stooge attr_reader :name, :hair, :habit def initialize(aRow) @name = aRow[0] @hair = aRow[1] @habit = aRow[2] end def baldish? hair.nil? end def muck_with_name name.gsub!(/y/, 'x') end def to_s "#<Stooge - name:#{name} hair:#{hair} habit:#{habit}>" end end #create an array of stooges @vstooges = STUFF.collect {|r| Stooge.new(r)} @stooges = STUFF.as(Stooge) puts @vstooges puts @stooges #'that' returns a collection of stooges #'are' expects a method that returns a boolean puts @stooges.that.are.baldish? puts @stooges.select {|s| s.baldish?} #'that' returns a collection of stooges #'have' expects a method that takes arguments puts @stooges.that.have.name == 'Mo' puts @stooges.select {|s| s.name == 'Mo'} #'extract' returns the result of the method puts @stooges.extract.name puts @stooges.collect {|s| s.name} #'all' and 'any' return a boolean #'are' as above puts @stooges.all.are.baldish? puts @stooges.any.are.baldish? #'all' and 'any' return a boolean #'are_not' is just as you expect puts @stooges.all.are_not.baldish? puts @stooges.any.are_not.baldish? #'all' and 'any' return a boolean #'have' expects a method that takes arguments puts @stooges.all.have.name == 'Mo' puts @stooges.any.have.name == 'Mo' #these return the entire collection ordered as specified puts @stooges.in_order_of.name puts @stooges.in_reverse_order_of.name #'do' sends the method to each object @stooges.do.muck_with_name puts @stooges
This paste will be private.
From the Design Piracy series on my blog: