From: "Jesús Gabriel y Galán" Date: 2012-09-28T17:09:08+09:00 Subject: Re: calcaulation with unknown numbers of numbers and options fail On Fri, Sep 28, 2012 at 9:40 AM, Roelof Wobben wrote: > Hello, > > I have this script: > > def calculate (options , *numbers) > options << "add" if options.empty? > numbers.inject(0) { |sum, number| sum + number } if options == "add" > numbers.inject() { |sum, number| sum - number } if options == "subtract" > end > > But now I see this output in Rubymonks : > > defaults to addtion when no option is specifiedNoMethodErrorundefined method > `empty?' for 4:Fixnuminvoking calculate(4, 5, add: true) returns > 9NoMethodErrorundefined method `empty?' for 4:Fixnuminvoking calculate(-10, > 2, 3, add: true) returns -5NoMethodErrorundefined method `empty?' for > -10:Fixnuminvoking calculate(0, 0, 0, 0, add: true) returns > 0NoMethodErrorundefined method `empty?' for 0:Fixnum > > I think the problem is that I cannot take care that the right values are in > the right variable. > > > Anyone a tip how to solve this ? The options hash is the first parameter to your function, so pass it as the first parameter: calculate({:add => true}, 4,5) and also, you shouldn't check options == "add", but options[:add] == true or just options[:add] Jesus.