Ruby: Calling Class Method from Instance

Ruby: Calling class method from instance

Rather than referring to the literal name of the class, inside an instance method you can just call self.class.whatever.

class Foo
def self.some_class_method
puts self
end

def some_instance_method
self.class.some_class_method
end
end

print "Class method: "
Foo.some_class_method

print "Instance method: "
Foo.new.some_instance_method

Outputs:


Class method: Foo
Instance method: Foo

Calling an instance method from a class method

In order to call an instance method you need to create an instance of the object. In ruby calling 'new' creates an instance of a class.

You can change this line

puts "#{action}"

To this:

puts "#{new.action}"

.new inside of the class will make a new instance of the class you are in. Which will also run your initialize and decrease @@variable by one. The way the class is written right now we never past if @@variable <9 unless you are making instances of the class outside of the code you shared.

Overall this is a pretty odd class. It isn't common to use class variables like this. It is much more common to create an instance of a class, use instance variables/methods to house your logic.

Calling method within Class

class TestClass
# a class method
def self.test_method
puts "Hello from TestClass"
end

# an instance method
def test_method
puts "Hello from an instance of TestClass"
end
end

# call the class method
TestClass.test_method

# create and instance object of TestClass
instance_of_TestClass = TestClass.new

# call the instance method of the new object
instance_of_TestClass.test_method

how to access class method from instance method

The method looks to make little to no sense, because total_points is an instance method, so why would you want to present the all records points's sum in an instance method?

Beside the above note, use ActiveRecord's methods. You are looking for sum:

UserWeight.sum(:points)

In method:

self.class.sum(:points)

Ruby: Can I use instance methods inside a class method?

Short answer is no, you cannot use instance methods of a class inside a class method unless you have something like:

class A
def instance_method
# do stuff
end

def self.class_method
a = A.new
a.instance_method
end
end

But as far as I can see, format_date does not have to be an instance method. So
write format_date like

def self.format_date(date)
# do stuff
end

Ruby On Rails: Can I call a class method from a class method?

This is answered here: Calling a class method within a class

To re-iterate:

def self.method1(bar)
# Do things with bar
# Call to method2
method2( bar )
end

A full class example:

class MyClass
def self.method1(bar)
bar = "hello #{ bar }!"
method2( bar )
end

def self.method2(bar)
puts "bar is #{ bar }"
end
end

MyClass.method1( 'Foo' )

How to access class method from the included hook of a Ruby module

There'a a method_added callback you could use:

module MyModule
def self.included(includer)
def includer.method_added(name)
puts "Method added #{name.inspect}"
end
end
end

class MyClass
include MyModule

def foo ; end
end

Output:

Method added :foo

If you want to track both, existing and future methods, you might need something like this:

module MyModule
def self.on_method(name)
puts "Method #{name.inspect}"
end

def self.included(includer)
includer.instance_methods(false).each do |name|
on_method(name)
end

def includer.method_added(name)
MyModule.on_method(name)
end
end
end

Example:

class MyClass
def foo ; end

include MyModule

def bar; end
end

# Method :foo
# Method :bar


Related Topics



Leave a reply



Submit