Home > Net >  What happens when a method is used on an object created from a built in class?
What happens when a method is used on an object created from a built in class?

Time:08-07

I understand that classes are like mold from which you can create objects, and a class defines a number of methods and variables (class,instances,local...) inside of it.

Let's say we have a class like this:

class Person
  def initialize (name,age)
    @name = name
    @age = age
  end 
  
  def greeting
    "#{@name} says hi to you!"
  end 
end 

me = Person.new "John", 34
puts me.greeting

As I can understand, when we call Person.new we are creating an object of class Person and initializing some internal attributes for that object, which will be stored in the instance variables @name and @age. The variable me will then be a reference to this newly created object.

When we call me.greeting, what happens is that greeting method is called on the object referenced by me, and that method will use the instance variable @name that is directly tied/attached to that object.

Hence, when calling a method on an object you are actually "talking" to that object, inspecting and using its attributes that are stored in its instance variables. All good for now.

Let's say now that we have the string "hello". We created it using a string literal, just like: string = "hello".

My question is, when creating an object from a built in class (String, Array, Integer...), are we actually storing some information on some instance variables for that object during its creation?

My doubt arises because I can't understand what happens when we call something like string.upcase, how does the #upcase method "work" on string? I guess that in order to return the string in uppercase, the string object previously declared has some instance variables attached to, and the instances methods work on those variables?

CodePudding user response:

Sorry to disillusion you, but Ruby is not written in Ruby. It's written in C. A string may present itself as an object to you, but under the hood it is some C storage — and C has no instance variables, and no classes; it is not an object oriented language at all. The upcase method is mapped to a C function and simply produces a new sequence of characters. See https://github.com/ruby/ruby/blob/6d742c9412d444650d705b65bc2d5c850054c226/string.c#L7407 for the source code.

CodePudding user response:

My question is, when creating an object from a built in class (String, Array, Integer...), are we actually storing some information on some instance variables for that object during its creation?

Yes, we are, basically:

string = "hello" is shorthand for string = String.new("hello")

take a look at the following:

https://ruby-doc.org/core-3.1.2/String.html#method-c-new (ruby 3)

https://ruby-doc.org/core-2.3.0/String.html#method-c-new (ruby 2)

What's the difference between String.new and a string literal in Ruby?

You can also check the following (to extend the functionalities of the class):

Extend Ruby String class with method to change the contents

So the short answer is:

Dealing with built in classes (String, Array, Integer, ...etc) is almost the same thing as we do in any other class we create

  • Related