I'm reading the Pickaxe book on Ruby and I've come across this example:
class TaxCalculator
def initialize(name, &block)
@name, @block = name, block
end
def get_tax(amount)
"#@name on #{amount} = #{@block.call(amount)}"
end
end
tc = TaxCalculator.new ("Sales tax") {|amt| amt * 0.075 }
tc.get_tax(100) # => "Sales tax on 100 = 7.5"
tc.get_tax(250) # => "Sales tax on 250 = 18.75"
The point of the example is to illustrate that if you prefix the last parameter in a method definition with an ampersand, any associated block is coverted to a Proc object, and that object is assigned to the parameter.
Now, that all makes sense, but I'm stumped on one thing: why do you need to assign name and block to @block in the initialize method? Why do you need name?
To me it seems like this should work:
def initialize(name, &block)
@name, @block = block
end
But, when I try that in irb @block becomes nil and I get an error, NoMethodError (undefined method "call" for nil:NilClass).
I don't understand what name is doing in that @block assignment. It makes sense that you would want to pass "Sales Tax" to @name. But I don't see how "Sales Tax" is being used by the block.