M
Mike Papper
I would like to include a module into a 'static' class - such that all
the modules methods are class methods. The problem is that code in the
module that uses "self.class.XYZ' breaks and when this code is included,
ruby cannot find the XYZ method.
Here is an example:
module Basic
def methoda
puts "AAAAAAAAAAA"
end
def mb
self.class.cee
end
module ClassMethods
attr_writer :cee
def cee
puts "CCCCCCCCCCCCC"
end
end
end
class Use
extend Basic
def self.z
mb
end
end
If I do the following:
Use.z
I get this error:
NoMethodError: undefined method `cee' for Class:Class
from (irb):100:in `mb'
from (irb):116:in `z'
from (irb):119
--------------
The context for this is in Rails, I want to use
ActionController::UrlWriter.url_for outside of the controller. And
within a static class. However, it complains with this error:
NoMethodError: undefined method `default_url_options' for Class:Class
Heres the url_for method in ActionController::UrlWriter
def url_for(options)
options = self.class.default_url_options.merge(options) # ERROR
OCCURS HERE
url = ''
...
...
end
--------------
I think my understanding of modules and using extend (or include) is
faulty or at least doesnt cover the subtleties of when a modle uses
"self.class" and its included in another class, then what becomes of
self.class in this case??
Any input into how this stuff works in Ruby is appreciated,
Mike
the modules methods are class methods. The problem is that code in the
module that uses "self.class.XYZ' breaks and when this code is included,
ruby cannot find the XYZ method.
Here is an example:
module Basic
def methoda
puts "AAAAAAAAAAA"
end
def mb
self.class.cee
end
module ClassMethods
attr_writer :cee
def cee
puts "CCCCCCCCCCCCC"
end
end
end
class Use
extend Basic
def self.z
mb
end
end
If I do the following:
Use.z
I get this error:
NoMethodError: undefined method `cee' for Class:Class
from (irb):100:in `mb'
from (irb):116:in `z'
from (irb):119
--------------
The context for this is in Rails, I want to use
ActionController::UrlWriter.url_for outside of the controller. And
within a static class. However, it complains with this error:
NoMethodError: undefined method `default_url_options' for Class:Class
Heres the url_for method in ActionController::UrlWriter
def url_for(options)
options = self.class.default_url_options.merge(options) # ERROR
OCCURS HERE
url = ''
...
...
end
--------------
I think my understanding of modules and using extend (or include) is
faulty or at least doesnt cover the subtleties of when a modle uses
"self.class" and its included in another class, then what becomes of
self.class in this case??
Any input into how this stuff works in Ruby is appreciated,
Mike