是否可以在方法中达到方法?例如:
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
.我需要在main_method
内部达到这个simple_method
.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
你问的不是很清楚,但是如果你想定义类方法,为什么不只定义类方法呢?
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