class Foo
def to_s
Why aren’t existing methods overridden? And where could I have
looked to find out the answer for myself (perhaps somewhere in Ruby’s
source?)?
Because of the position in the inheritance hierarchy:
irb(main):001:0> module Bar;end
=> nil
irb(main):002:0> class Foo
irb(main):003:1> include Bar
irb(main):004:1> end
=> Foo
irb(main):005:0> Foo.ancestors
=> [Foo, Bar, Object, Kernel]
Methods defined in Foo are always found before their counterparts in
included modules. Consequently you can override super class methods
with a module.
Methods defined in Foo are always found before their counterparts
in included modules. Consequently you can override super class
methods with a module.
Aha, of course. Thank you for the clear explanation.
Methods defined in Foo are always found before their counterparts
in included modules. Consequently you can override super class
methods with a module.
Aha, of course. Thank you for the clear explanation.
You’re welcome!
I really should have worked that one out myself!
Ah, no worries. Once in a while this just happens to all of us.
Btw, you got a nice website there (with a typo on the vortex page g).
Are you a talented Ruby on Rails or mod_perl Developer looking for an
exciting opportunity? My client runs a leading enthusiasts
community/e-commerce site and they need your expertise to create new
features and functions related to social networking. This is a great
opportunity to work with a fun group and contribute to exciting
technology that’s used by many!
Qualifications:
Thorough understanding of object-oriented methodology
Thorough understanding of MVC architectures
Experience with coding database-backed web applications
Expert knowledge in either mod_perl or Ruby on Rails
Intermediate knowledge of a mod_perl templating language
Intermediate CSS and Javascript skills
Willingness to work in an Agile development environment
Ability to clearly and concisely communicate technical ideas
If you are interested or know someone that is interested please have
them email a resume as a Word attachment to [email protected] or [email protected]
this allows you to both override and super up, in any combination,
with a method injected late into a class hierarchy
Actually whenever these issues come up I wonder why we still define
methods in classes?
Either, using pure Ruby implementening all functionality im Modules
(with the dangerous pitfall of double inclusion, or just using Traits
Composition would just end all of that kind of complexity) But even I
do not use my Traits package, old habits are difficult to lose
indeed…
Cheers
Robert
– http://ruby-smalltalk.blogspot.com/
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein
i personally avoid aliases like that - they stack when you double
require or double include a module and throw into a loop. this kind
of thing can be done safely and generically using a variable for the
previous method that’s protected from the gc and a lookup in the new
method:
cfp:~ > cat a.rb
class A
def foo
p “A.foo”
end
end
class B
end
module M
NoGC = []
def self.included other
other.module_eval do
if((foo = instance_method ‘foo’ rescue false))
NoGC.push foo
supra = “ObjectSpace._id2ref(#{ foo.object_id }).bind
(self).call(*a, &b)”
end
eval <<-code
def foo *a, &b
#{ supra }
p “M.foo”
end
code
end
end
end
A.send :include, M
B.send :include, M
A.new.foo
B.new.foo
cfp:~ > ruby a.rb
“A.foo”
“M.foo”
“M.foo”
this allows you to both override and super up, in any combination,
with a method injected late into a class hierarchy
i personally avoid aliases like that - they stack when you double
require or double include a module and throw into a loop. this
kind of thing can be done safely and generically using a variable
for the previous method that’s protected from the gc and a lookup
in the new method:
I see. That sounds much more sensible.
NoGC = []
#{ supra }
A.new.foo
with a method injected late into a class hierarchy
Wonderful!
I should confess that I haven’t entirely convinced myself that I
understand your construction of supra. Given that you have the
unbound method in the foo variable, is the reason why you can’t bind
foo directly, perhaps like this:
supra = "#{foo}.bind(self).call(*a, &b)"
…because there’s no way to write the string so it eval’s the way we
want it to?
Btw, you got a nice website there (with a typo on the vortex page g).
Thanks! You are kind to say so.
You’re welcome! I really like the clean design. You know, I am more
the “Braun” type than the “Hundertwasser” type. Also, I noticed you use
Mongrel for hosting.
I can’t find that typo though…are you pulling my leg?
I think he was joking since Americans typically use “specialize” and
“center” instead
Nope. First, I am not American* and second, these are not the words I
had in mind. The “offending” word is somewhere between “organised” and
“David Allen’s”. To give an additional hint, it’s spelled correct and
wrong in the same paragraph.
Cheers
robert
Although I have to say that most English I am exposed to is probably
American English.
The “offending” word is somewhere between “organised” and “David
Allen’s”. To give an additional hint, it’s spelled correct and
wrong in the same paragraph.
Got it. Quelle gaffe!
I like to take pride in shunning automatic spell-checking…so I
shouldn’t be surprised when a fall follows