slindsey3000
slindsey3000

Reputation: 4269

Ruby singleton class

I am unsure about the difference between this.

def String.hello  
  puts "hello there"   
end

and

x = Person.new    
def x.hello    
  puts "hello there"    
end

From my understanding the second code block will create an object of class Person. When I do the def x.hello it creates an anonymous class (singleton class) that will be checked first for methods when sending a message to the x object.

Is this the same case for the def String.hello? String is just an instance of class Class correct? I have read that doing def String.hello will add the method as one of String's class methods.... this would be different than an anonymous class being created that sits in between the object and its class where it gets its instance methods.

What happens with both blocks of code above?

Upvotes: 3

Views: 3338

Answers (3)

Guilherme Bernal
Guilherme Bernal

Reputation: 8293

Just to add to the Matt's answer:

Both examples do the same thing, Writting them in another way:

String = Class.new # < done inside ruby initialization    
def String.hello    
  puts "hello there"    
end

and

x = Person.new    
def x.hello    
  puts "hello there"    
end

On Ruby you can add methods to a class, created with A = Class.new or with the syntax sugar class A; ...; end, or to a Eigenclass, that exists for every object. Class methods are, on really, methods of the Eigenclass of an instance of Class, think about what is "self" in def self.method; ...; end. Eigenclasses can be opened with this sintax:

x = Person.new
class << x
  # ...
end

As Eigenclasses are also instances of class (try to add p self.class on last example) they also have Eigenclasses and so on. If it appears to be confusing, just remember that Object is a class and Class is an object. This is why I love Ruby!

Upvotes: 2

Matt Briggs
Matt Briggs

Reputation: 42188

I love this part of ruby. There is this beautiful symmetry where most of the core functionality is just sugar over the advanced functionality, so once you fully grok a concept, you can apply that understanding to a lot of the language.

Is this the same case for the def String.hello? String is just an instance of class Class correct?

Yes, you are creating an instance of Class, and assigning it to a constant.

I have read that doing def String.hello will add the method as one of String's class methods.... this would be different than an anonymous class being created that sits in between the object and its class where it gets its instance methods.

Nope, the piece you are missing is thinking its possible to have a class level method WITHOUT adding it to a singleton class. What you have is an object that is an instance of Class, and you are adding methods to an implicit class that sits inbetween it and Class. You will also see this syntax sometimes

class << self
  def method
  end
end

That is doing the same thing, just being very explicit about it.

Upvotes: 6

Deleteman
Deleteman

Reputation: 8700

The following code will add the "hello" method to the String class, that way all following strings will have the "hello" method:

def String.hello  
   puts "hello there"   
end

Whilst the following code, will add the "hello" method, to the instance x of the class Person. If you create a new Person, that object will not have the "hello" method.

x = Person.new    
def x.hello    
  puts "hello there"    
end

That is the main difference, as I understand it.

Does this help?

Upvotes: 0

Related Questions