Calling a class method within a class

I realize this perhaps a naive question but still I cant figure out how to call one method from another in a Ruby class.

i.e. In Ruby is it possible to do the following:

class A
   def met1
   end
   def met2
      met1 #call to previously defined method1
   end
end

Thanks,

RM

Answers


Those aren't class methods, they are instance methods. You can call met1 from met2 in your example without a problem using an instance of the class:

class A
   def met1
     puts "In met1"
   end
   def met2
      met1
   end
end

var1 = A.new
var1.met2

Here is the equivalent using class methods which you create by prefixing the name of the method with its class name:

class A
   def A.met1
     puts "In met1"
   end
   def A.met2
      met1
   end
end

A.met2

Need Your Help

hide active click events and then to reappear

javascript jquery

I have a series of buttons that show and hide corresponding divs. I would like the buttons, once clicked, to be hidden but to show again when another button is clicked. I hope that the following pl...

ORDER BY lower case of output column

sql postgresql sql-order-by postgresql-9.1

I'm able to run this Postgres query without any issue: