I got the following code from #avdi's Ruby Tapas episode for today:
module Eventful
def self.included(other)
other.extend(Macros)
end
def add_listener(listener)
(#listeners ||= []) << listener
end
def notify_listeners(event, *args)
(#listeners || []).each do |listener|
listener.public_send("on_#{event}", *args)
end
end
module Macros
def event(name)
module_eval(%Q{
def #{name}(*args)
notify_listeners(:#{name}, *args)
end
})
end
end
end
class Dradis
include Eventful
event :new_contact
end
class ConsoleListener
def on_new_contact(direction, range)
puts "DRADIS contact! #{range} kilometers, bearing #{direction}"
end
end
dradis = Dradis.new
dradis.add_listener(ConsoleListener.new)
dradis.new_contact(120, 23000)
I understand the concept of events and listeners and the observer pattern, but don't get how/why this syntax is working, and haven't seen it in any manuals. The class Dradis has this:
event :new_contact
At first, I thought that event was a method and :new_contact was an argument so that I would call event on an instance of Dradis, something like:
dradis = Dradis.new
dradis.event
but instead, new_contact is called on an instance of Dradis like:
dradis = Dradis.new
dradis.add_listener(ConsoleListener.new)
dradis.new_contact(120, 23000)
and that triggers the event method in the Macro module.
Can anyone explain why it works like this? calling :new_contact on an instance dradis to trigger the event method?
I didn't watch the episode, but look, it's right there.
module Macros
def event(name)
module_eval(%Q{
def #{name}(*args)
notify_listeners(:#{name}, *args)
end
})
end
end
event is a method which defines another method (new_contact) which calls notify_listeners from Eventful.
and that triggers the event method in the Macro module
Incorrect. That method has finished its work a long time ago and it doesn't get invoked again. It produced a new method using module_eval / def and that new method (new_contact) is what's getting called.
It's important to understand that event method runs only once, when the Dradis class is parsed and loaded. It does not get run on every instantiation of Dradis.
Several separated features of ruby is used: In the line event :new_contact the "evnet" is class method (http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html#UE).
Usually class methods are defined by:
class A
def A.my_class_method
#code
end
end
A.my_class_method #executing class_method
or
class A
def <<self #usefull when you want delare several class methods
def A.my_class_method
#code
end
end
end
A.my_class_method #executing class_method
In the code the method is included by the module Macros.
The key thing is, that (class method) event is dynamicaly creating new (instance) method (in this case new_contact)
The name of the method is passed as argument to event). And this method providing calling of the listener.
Can anyone explain why it works like this? calling :new_contact on an instance dradis to trigger the event method?
This is by the dynammically created method new_contact which is calling notify_listeners(:#{name}, *args)
Related
In the Ruby programming language, I am creating a class with a class-level macro, as follows:
class Timer
def self.add_time
def time
STDERR.puts Time.now.strftime("%H:%M:%S")
end
end
end
The class method add_time, when executed, will generate a time method.
Now, I can execute that class-level macro in another class Example as follows:
class Example < Timer
add_time
end
When I now call time on an instance of class Example, the time method is present there, as I intended:
ex = Example.new
ex.time
and prints the current time: 23:18:38.
But now I would like to put the add_time macro in a module and still have the same overall effect. I tried with an include like this:
module Timer
def self.add_time
def time
STDERR.puts Time.now.strftime("%H:%M:%S")
end
end
end
class Example
include Timer
add_time
end
ex = Example.new
ex.time
but then I receive an error that the method add_time is not defined on the class Example: NameError: undefined local variable or method ‘add_time’ for Example:Class. So then I tried with an extend instead like this:
class Example
extend Timer
add_time
end
but it gives me a similar error.
So the question is: How can I get the same effect as in my original example where the Timer was defined as a class, but using a module instead?
As #CarySwoveland pointed out, the method def self.add_time in the module Timer gets disregarded upon inclusion or extension in a class. Only the module's instance methods are added to the class as instance method of the class (in case of inclusion) or as class methods of the class (in case of extends).
module Timer
def add_time # INSTANCE METHOD !
def time
STDERR.puts Time.now.strftime("%H:%M:%S")
end
end
end
So the first step of the solution is to declare the method def add_time as an instance method of the module. Next, we extend the class Example with that module, so that the module's instance method gets added as a class method in the class Example, and we call the add_timemethod:
class Example
extend Timer # EXTEND INSTEAD OF INCLUDE
add_time
end
However, this doesn't quite work as desired yet as the time method has now been generated as a class method: Example.time prints the current time 01:30:37, but an instance ex of class Example does not understand the method time.
The solution is thus to generate the method def time as an instance method rather than as a class method. This can be done using class_eval, which leads us to the following working solution:
module Timer
def add_time # INSTANCE METHOD !
self.class_eval do # USE class_eval TO DEFINE AN INSTANCE METHOD !
def time
STDERR.puts Time.now.strftime("%H:%M:%S")
end
end
end
end
class Example
extend Timer # USE EXTEND TO ADD add_time AS A CLASS METHOD
add_time
end
ex = Example.new
ex.time
I am new to ruby and I'm hoping someone can help me. I am writing tests using test::unit and within my tests I needed to run some code before an assert was called so I overrided the assert methods like so:
class TestSomething < Test::Unit::TestCase
def assert_equal(expected, actual, message = nil)
mycode ..
super(expected, actual, message)
end
def assert(object, message)
my code ...
super(object, message)
end
def assert_not_nil(object, message = "")
my code ...
super(object, message)
end
def setup
end
def test_case1
end
def test_case1
end
def teardown
end
end
The above structure works fine and the asserts call my code. The thing is I have 100s of test classes. The assert overriding will be the same for all of them. Do I have to copy the assert overrides to the top of every class or is there a way for all of them to get the assert overrides in one go?
One more question. Is there a way of catching an error if it occurs anywhere within the entire class?
A
I am not sure why you do not want to use before filter, but the question as it was stated has an answer: since ruby classes are open, one might do the following.
class Test::Unit::TestCase
# store the original
alias_method :assert_equal_original, :assert_equal
# override
def assert_equal *args
# mycode ..
assert_equal_original *args
end
# the same for other methods
end
Once it is done, any derived class will call my_code before calling the original method.
To catch an error within the scope of the class is impossible, AFAIK.
I want to call some_instance_method from some_class_method. In the following code, the class method is being called from the instance method.
class Foo
def self.some_class_method
puts self
end
def some_instance_method
self.class.some_class_method
end
end
Does what you want to do really make sense? Which instance should the instance method be called on? If it doesn't matter, you can do this:
class Foo
def self.some_class_method
new.some_instance_method
puts self
end
def some_instance_method
self.class.some_class_method
end
end
This will cause an infinite loop, of course.
To call an instance method you need to have that object already instantiated somewhere, you can't call an instance method without an object.
def self.some_class_method
puts self.first.some_instance_method
end
Something like this should work
You need to pass the instance you want to be the recipient of the call to the class method. So the new code is:
class Foo
def self.some_class_method(instance)
instance.some_other_instance_method
puts self
end
def some_instance_method
self.class.some_class_method(self)
end
end
Notice that the class method is calling 'some_other_instance_method'. If the class method were to call the same method that called it, it would enter an endless loop.
For example
class Foo
def bar
end
end
In that code, bar would only be available within any instance of the class.
Is it possible to change the execution context of the method to the Eigenclass
without changing how the method itself is defined so that the method is now available as a singleton without ever needing to call self.new?
Preferably I would like to do it any of the code that doe this to code that is added in via a class that Foo could inherit from.
At the moment what I'm doing amounts to:
class Test
def method_added method
self.define_singleton_method method do
self.new.send method
end
end
end
and for what I need this doesn't work as I'm changing the execution context by calling new.
You can simply do:
class Test
def self.method_added method
module_function method
end
end
class A < Test
def foo
:hello
end
end
A.foo #=> :hello
I worked out how to do it just now >_<.
Here's the code:
class Test
def method_added method
m = self.new.method(method) #get method object
self.define_singleton_method(method) do #create method with same name within the singleton class
m.call #call the block which will now run the code of the added method within the context of the Eigenclass/Singleton
end
end
end
So what it does is it grabs a method object form an instance and then calls that method as a block within the context of the class.
So the first code example becomes:
class Foo < Test
def bar
end
end
and the method bar can now be accessed as
Foo.bar rather then Foo.new.bar which means no instance creation; besides the time it does within method added, but that's fine as it's the only way to get the method object as far as I'm aware.
Which is why it's probably best to create an instance only the once when the class is inherited (within def self.inherited), store it within the class and then just access that instead of calling self.new.
I have a sidekiq worker class. I currently implemented it this way. It works when I call PROCESS, and it will queue the method called PERFORM. But i would like to have more than one method that I can queue.
As a side note, is there a difference doing this and simply doing SocialSharer.delay.perform?
# I trigger by using SocialSharer.process("xxx")
class SocialSharer
include Sidekiq::Worker
def perform(user_id)
# does things
end
def perform_other_things
#i do not know how to trigger this
end
class << self
def process(user_id)
Sidekiq::Client.enqueue(SocialSharer,user_id)
end
end
end
SocialSharer.delay.perform would delay a class method called perform. Your perform method is an instance method.
Workers are designed to be one class per job. The job is started via the perform method. You can use delay to kick off any number of different class methods on a class, like so:
class Foo
def self.a(count)
end
def self.b(name)
end
end
Foo.delay.a(10)
Foo.delay.b('bob')
Well, if you really want to have all "performable" methods in one class, I'd suggest you to rename perform method to something different (say, perform_something), and create a new perform method which would dispatch the control flow:
class SocialSharer
include Sidekiq::Worker
# the 3 lines below may be replaced with `alias_method :perform, :public_send`
def perform(method, *args)
self.public_send(method, *args)
end
def perform_something(user_id)
# does things
end
def perform_other_things
# does other things
end
def self.process(user_id)
Sidekiq::Client.enqueue(SocialSharer, :perform_something, user_id)
Sidekiq::Client.enqueue(SocialSharer, :perform_other_things)
end
end