Skip to content

Instantly share code, notes, and snippets.

@DanielVartanov
Last active December 29, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DanielVartanov/7741198 to your computer and use it in GitHub Desktop.
Save DanielVartanov/7741198 to your computer and use it in GitHub Desktop.
class Array
module ToProc
def to_proc(*args)
lambda do |*args|
object = args.shift
each { |message| object = object.__send__ message }
object
end
end
end
include ToProc
end
### Usage example
Publisher = Struct.new(:name)
Book = Struct.new(:publisher)
Review = Struct.new(:book)
def build_publisher(name)
Review.new(
Book.new(
Publisher.new(name)
))
end
reviews = [build_publisher('Publisher 1'), build_publisher('Publisher 2')]
p reviews.map(&[:book, :publisher, :name]) # => ["Publisher 1", "Publisher 2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment