digitalwhitebyte wrote:def self.method_name
end
defines a class method
def method_name
end
defines an instance method
If you are within the namespace of a class, then yes that is correct...
Code: Select all
class myKlass
# in here, self = myKlass
def self.method
# in here, self = myKlass.
end
def method
# in here, self=the object instance
end
end
But if it is not wrapped in a class definition, then self = the RubyEdit instance, so you are defining a singleton method of the RubyEdit object...
Code: Select all
def self.hello
"hello"
end
def goodbye
"goodbye"
end
hello #=> "hello"
goodbye #=> "goodbye"
singleton_methods #=> ":loadState, :saveState, :hello, :goodbye"
So if you are not within a Class/Module definition context, adding 'self' makes no difference, because the RubyEdit instance is the implicit 'self' anyway.
The class in the RubyEdit instance context is a proxy singleton class for the individual object which inherits from the RubyEdit base class. You can do something similar for any object...
Code: Select all
a = "hello"
b = "goodbye"
a.define_singleton_method(:ouch){"a is #{self}, ouch"}
a.ouch #=> "a is hello, ouch"
b.ouch #=> <undefined method "ouch" for "goodbye"; String>
So we've added a method that only applies to string 'a' - which will also continue to respond to any other String instance methods, as String is still its superclass. String 'b' remains unaffected because the base class has not changed in any way.
This is effectively what is happening when we create a "classless" definition inside a Ruby primitive - everything is executed in the context of the instance, not the class.
This is actually pretty clever work by the DSPr boys - it's what allows every Ruby editor access to all of the built-in methods, while letting us define our own methods which are only visible to the individual primitive.
The next step will be to similarly isolate plugins/schematics from each other to avoid class/nodule sharing- I've emailed Malc with a couple of ideas for how this might be achieved, and he sounded positive about it, so I'm hopeful that we'll see that improved some time soon.