Bindings, Platforms, and Innovation
This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.
Tracking change and innovation in the enterprise software development community
Posted by Werner Schuster on Feb 26, 2008 01:30 PM
Next to Ruby 1.9's big new features such as threading changes or Fibers, it also added a host of smaller utility functions to the standard library. We already mentioned theObject.tap method, which adds a convenient way of spying on chained method calls. tap method, the idea for the to_proc has been known in the Ruby community for some time - the feature is also available for pre-1.9 Ruby versions. Ruby 1.9 simply integrates this into the class Symbol, making the behavior available without any supporting libraries. to_proc makes code like (1..100).inject(&:+) work: The & operator converts Proc objects into blocks and block into Proc objects. In this case, it tries to convert the symbol :+ into a block. The conversion uses Ruby's built-in coercion mechanism. That mechanism checks to see whether we have a Proc object. If not, it sends the #to_proc method to the argument to make a Proc. If the Symbol :+ has a #to_proc method, it will be called. In Ruby 1.9 it has a #to_proc method. That method returns a Proc that takes its first argument and sends the + method to it along with any other arguments that may be present.This beheavior can be seen with code like this.:
So,&:+really means{ |x, y| x + y }.
plus = :+.to_procSince the class
puts plus.call(1,2) # prints '3'
Symbol has the to_proc method, this approach works for every symbol:to_s = :to_s.to_proc
to_s.call(42) # results in the string "42"
to_proc is simple. Dave Thomas (PragDave) shows how it works:
def to_proc
proc { |obj, *args| obj.send(self, *args) }
end
It creates a Proc which, when called on an object, sends that object the symbol itself. So, when names.map(&:upcase) starts to iterate over the strings in names, it'll call the block, passing in the first name and invoking its upcase method.
to_proc to standard Ruby 1.9, simply because the Symbol now always comes with to_proc.Symbol and added it. Also: understanding the code involved knowledge of the general Symbol#to_proc idiom, which in Ruby 1.9 has been officially added, and is now more likely to be mentioned in documentation. (1..100).map(&:to_s)Vs.
(1..100).map{|x| x.to_s }
Saving five characters (for this example) - is it worth the extra complexity or not? Proc#curry. A recent ruby-core discussion shows what Proc#curry does:
It's not difficult at all,
proc {|x, y, z| x + y + z }.curry
returns the proc object equivalent to
proc {|x| proc {|y| proc {|z| x + y + z } } }
[..] is the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the other arguments having been specified by the curry).In other words: using currying, a Proc that takes
x arguments, can be called with a single argument. Since, obviously, it can't return the result of it's code - it's missing arguments required to run the code - it returns a new Proc that takes (x - 1) arguments. Once this has been repeated enough times so the resulting Proc has all the arguments it needs, the code is evaluated and the result returned. plus_five = proc { |x,y,z| x + y + z }.curry.call(2).call(3)
plus_five[10] #=> 15
Note: plus_five is a Proc - the [] operator for Proc is overloaded to invoke the Proc. Proc#curry is a very recent addition to Ruby 1.9 - to try it, you'll have to use a recent revision.Give-away eBook – Confessions of an IT Manager
Agile Development: A Manager's Roadmap for Success
Effective Management of Static Analysis Vulnerabilities and Defects
The Agile Business Analyst: Skills and Techniques needed for Agile
This presentation focuses on the Internet and separating myth from fact, history from the future, and the mundane from the imaginative. Bob Frankston presents a vision of what could and should be.
This article explores the use of JBoss and jBPM to implement design solutions that effectively address the issue of orchestrating long running activities.
This presentation covers the use of graph databases as an optimal solution for data that is difficult to fit in static tables, rapidly evolving data or data that has a lot of optional attributes.
This session introduces Real Options and shows how it can help in running your project. Real Options is a decision-making process that can be used to manage risk.
This article discusses the use of bindings on services and references (including the instance of non-configured bindings) as the means to implement SCA communications in a Web and SOA environment.
After a short introduction to DSLs, Scott Davis plays with the keyboard showing how to approach the creation of a DSL by typing working snippets of Groovy code that get executed.
IBM Rational and InfoQ present, Scaling Agile with C/ALM, an eBook showing organizations how to become “finely tuned software delivery machines” by enabling team integration and scaling.
Amanda Laucher presents a real life enterprise application written in F#. She shows actual code snippets, explaining design decisions and suggesting how to use some of the F# constructs.
No comments
Watch Thread Reply