Take the 2-minute tour ×

Lets say I want a method called adder that will make it so that i can:

5.adder => 5

5.adder(3) => 8

20.adder(6) => 26

I have looked and not found. The logic is not that tricky, and i kinda get how procs work, but the fact that i have to be able to call the method on a integer is proving new and hard to find more me, thanks.

I have tried a bunch of things, like:

def adder(x = 1)

     Proc.new { |i| i + x }


and here is the rspec test i want to pass

  describe "adder" do

it "adds one to the value returned by the default block" do
  adder do
  end.should == 5

it "adds 3 to the value returned by the default block" do
  adder(3) do
  end.should == 8

nevermind, found the aweser, i was thinking i would have to mutate a class and all it was

 def adder(adds = 1, &block)

block.call + adds

share|improve this question
Ruby is an OOP language. –  sawa Apr 7 '13 at 4:21
Do you want 5.adder to return 5 or a function that can be called again to add a number to it? –  Dogbert Apr 7 '13 at 4:22
Want it to return 6. someone below answered, so no worries. –  user2251284 Apr 9 '13 at 17:01

Your Answer



Browse other questions tagged or ask your own question.