From: Brian Candler Date: 2009-02-03T20:29:13+09:00 Subject: Re: choose parameter to solve for Jason Lillywhite wrote: > I'm looking for a clean way to write a function that allows me to choose > which parameter to solve for assuming all others are known. Is it good > design to do something like this: ? > > class Foo > def initialize(x, y, z) > @x, @y, @z = x, y, z > end > > def function(var) > if @x.nil? > @x = var > elsif @y.nil? > @y = var > else > @z = var > end > @x + @y + @z > end > > end > > z = Foo.new(1, 2, nil) > z.function(3) > => 6 Hmm, is this some sort of currying? adder = lambda { |x,y,z| x+y+z } z = lambda { |z| adder[1,2,z] } z[3] You could perhaps factor this out into some sort of fix_args method. Ruby 1.9 has Proc#curry, but I think this only fixes from the left. -- Posted via http://www.ruby-forum.com/.