How can call a class method from another class method

Hello,

I have this

class Calc

def self.one
  1
 end

def self.add
   -> (y) { x+y}
 end

end

but when I do

Calc.one.add.one

I see a Nomethoderror because 1 does not have the method add.

Roelof

Because the one method returns a Fixnum (1) and Fixnum doesn’t have the
method add.
One way to solve that is to store 1 in a class variable and let one
return
self.

Something like:

def self.one
@@first_number = 1
self
end

If you are trying to solve the fluentcalculator quiz I have a solution
here:

bye
ema

Emanuele DelBono schreef op 14-6-2014 16:26:

def self.one
@@first_number = 1
self
end

Still it do not work.

I have now this :

class Calc

def self.one
@@first_number = 1
self
end

def self.add
-> (y) {x+y}
end

end

Calc.one.add.one

And now I see this errror message

main.rb:16:in ': undefined methodone’ for # (NoMethodError)

Roelof

Now your problem is the add method that returns a lambda, and lambdas
doesn’t have the method one.
Why do you use a lambda? I don’t think it could work for what you’re
doing,
but maybe some ruby-expert could help us.

Calc.one.add.one

So you expect one() to return Calc the first time it is called, and then
you expect one() to return an integer the second time it is called?

class Calc
@register = []
@count = 0

class <<self
attr_accessor :register
end

def self.one
@count += 1
@register << 1

if @count == 1
  self
else
  eval @register.join(" ")
end

end

def self.add
@register << “+”
self
end

end

puts Calc.one.add.one #=>2