Skip to content

Instantly share code, notes, and snippets.

@sergueif
Created April 14, 2012 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergueif/2381626 to your computer and use it in GitHub Desktop.
Save sergueif/2381626 to your computer and use it in GitHub Desktop.
message/block chaining
#ChronicDuration is just a random library that parses/outputs strings of time
#The meat of this the last expression/statement
%w{foo bar baz} # ["foo", "bar", "baz"]
ChronicDuration.parse("1:30") # 90
ChronicDuration.output(90) # "1:30"
class Object
def pat(&block) # backwards of 1.9.2 method "tap" but arbitrary name
block.call self
end
end
[1,-2,-3].map(&:abs) # [1,2,3] converts symbol (a message) to a Proc (kind of like block)
(-5).pat(&:abs) # 5 # see above
%w{1:30 0:15}.
map(&ChronicDuration.method(:parse)).
reduce(&:+).
pat(&ChronicDuration.method(:output)) # 1:45
@agrimm
Copy link

agrimm commented Apr 18, 2012

Code golfing: reduce doesn't require an ampersand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment