Last active
December 29, 2015 23:09
-
-
Save DanielVartanov/7741198 to your computer and use it in GitHub Desktop.
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
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