From: Brian Candler Date: 2009-02-03T22:04:43+09:00 Subject: Re: choose parameter to solve for Jason Lillywhite wrote: > 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 Also, if this is really the pattern you want (an object holding default values for each of the args), then you probably don't want to modify the instance variables between invocations. So here is a more general version: class Foo def initialize(*args) @args = args end def function(*more) # for each nil argument, pick one from 'more' calculate(*@args.collect { |x| x || more.shift }) end def calculate(x,y,z) x + y + z end end z = Foo.new(1,2,nil) z.function(3) q = Foo.new(nil,nil,6) q.function(4,5) But I still think some flavour of currying is what you want. Is it important that arbitrary parameters are fixed, rather than just the left-most ones? -- Posted via http://www.ruby-forum.com/.