instance-method

How to understand the difference between class_eval() and instance_eval() ?

Foo = Class.new Foo.class_eval do def class_bar "class_bar" end end Foo.instance_eval do def instance_bar "instance_bar" end end Foo.class_bar #=> undefined method ‘class_bar’ for Foo:Class Foo.new.class_bar #=> "class_bar" Foo.instance_bar #=> "instance_bar" Foo.new.instance_bar #=> undefined method ‘inst...

Why does instance_eval() define a class method when called on a class?

Foo = Class.new Foo.instance_eval do def instance_bar "instance_bar" end end puts Foo.instance_bar #=> "instance_bar" puts Foo.new.instance_bar #=> undefined method ‘instance_bar’ My understanding is that calling instance_eval on an object is supposed to allow you to define an instance variable or method for that object...

Cocoa-Touch. What Exactly is the Difference Between These NSMutableData Methods?

One thing I'm a bit unclear on is the difference between these NSMutableArray Methods: // Class Method Style NSMutableData *myMutableDataInstance = [NSMutableData dataWithLength:WholeLottaData]; and // Instance Method Style NSMutableData *myMutableDataInstance = nil; myMutableDataInstance = [[[NSMutableData alloc] initWithLength:...

Overcoming Python's limitations regarding instance methods

It seems that Python has some limitations regarding instance methods. Instance methods can't be copied. Instance methods can't be pickled. This is problematic for me, because I work on a very object-oriented project in which I reference instance methods, and there's use of both deepcopying and pickling. The pickling thing is done mos...

Delegating instance methods to the class method

In Ruby, suppose I have a class Foo to allow me to catalogue my large collection of Foos. It's a fundamental law of nature that all Foos are green and spherical, so I have defined class methods as follows: class Foo def self.colour "green" end def self.is_spherical? true end end This lets me do Foo.colour # "green" ...