Created
July 28, 2010 20:07
-
-
Save coldnebo/496081 to your computer and use it in GitHub Desktop.
blog: how to call lambdas without call()
This file contains 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
# inspired by the protovis library pv.Scale.linear().range() | |
# adapted from rspec codebase | |
def let(name, scale_function) | |
Kernel.send :define_method, name do |p| | |
scale_function.call(p) | |
end | |
end | |
# returns a mapping function from the desired domain (x1,x2) to the desired range (y1,y2) | |
def scale(x1,x2,y1,y2) | |
lambda {|p| Integer(((y2-y1)*((p-x1)/Float(x2-x1)))+y1) } unless (x2-x1) == 0 | |
end | |
let(:y, scale(200,400,0,100)) | |
puts y(200) | |
puts y(400) | |
puts y(202) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from my blog article by the same name.