Skip to content

Instantly share code, notes, and snippets.

@coldnebo
Created July 28, 2010 20:07
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 coldnebo/496081 to your computer and use it in GitHub Desktop.
Save coldnebo/496081 to your computer and use it in GitHub Desktop.
blog: how to call lambdas without call()
# 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)
@coldnebo
Copy link
Author

coldnebo commented Nov 5, 2011

from my blog article by the same name.

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