Skip to content

Instantly share code, notes, and snippets.

@coldnebo
Created July 28, 2010 20:07
Show Gist options
  • Select an option

  • Save coldnebo/496081 to your computer and use it in GitHub Desktop.

Select an option

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

coldnebo commented Nov 5, 2011

Copy link
Copy Markdown
Author

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