是否有可能在方法中达到方法?例如:
class HardWorker < WebsocketRails::BaseController
def perform
self.main_method
end
def main_method
puts "main method"
def simple_method # how to call this from outisde?
puts "simple method"
end
def another_method
puts "another_method"
# do stuff
end
another_method #start running "another method in background"
end
end
我需要在“主要方法”中找到“simple_method”。
WebsocketRails::EventMap.describe do # now it works like that:
subscribe :event_name, :to => HardWorker, :with_method => :main_method
end
触发:event_name
后,我的控制台上显示"main method"
。但我需要在那里写"simple method"
,而无需重新启动main_method
。我需要在simple_method
内找到此main_method
。 Becouse main_method
已经在后台运行了,我需要在其中找到一个方法并做很多计算。我使用sidekiq,所以我不能使用main_method
范围之外的全局变量。我想我需要它来工作:
WebsocketRails::EventMap.describe do # i wish it works, but it doesn't
subscribe :event_name, :to => HardWorker, :with_method => :main_method[:simple_method]
end
更新:我需要更新这个@global_object。如果我记得“main_method”,我会松开本地增加的@global_object。我需要它在本地增加,但我不记得main_method。
def main_method
@global_object = 0
def simple_method
@global_object += 100
end
def another_method
(1..(2**(0.size * 8 -2) -1)).each do |number|
# every second updating my data and sending to Redis DB
@global_object++
sleep 1
end
end
another_method
end
答案 0 :(得分:1)
你要问的不太清楚,但是如果你想定义类方法,为什么不定义类方法?
class HardWorker < WebsocketRails::BaseController
def perform
self.main_method
end
def self.main_method
puts "main method"
self.simple_method
self.another_method #start running "another method in background"
end
def self.simple_method # how to call this from outisde?
puts "simple method"
end
def self.another_method
puts "another_method"
# do stuff
end
end