Rituraj Saha
Rituraj Saha

Reputation: 1

Blocks in Ruby, Error:Wrong Number of arguments

def power(a,b)
    puts" #{a}**#{b} is"
    yield a,b
    puts"Program Terminating..."
end

power {|a,b| printf "#{a**b}" }

power(2,3)

The expected output should be 8.

Error message:

`power': #wrong number of arguments (given 0, expected 2) (ArgumentError)

I want to use a block with multiple parameters that is invoked from a method that accepts two arguments.

Upvotes: 0

Views: 410

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

You basically have two calls to power without any reason (I’d wild guess you expected kinda currying there, but it’s not how currying is done in ruby.)

The first call to power with a block does not pass any argument, while both regular arguments are mandatory. There should be a single call, passing both a block and arguments:

power(2, 3) { |a, b| print "#{a**b}" }

Upvotes: 8

Related Questions