Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 588
slindsey3000 Avatar asked Mar 01 '26 12:03

slindsey3000


2 Answers

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.

like image 53
Matt Briggs Avatar answered Mar 04 '26 18:03

Matt Briggs


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!

like image 30
Guilherme Bernal Avatar answered Mar 04 '26 20:03

Guilherme Bernal