Kang-Kyu Lee

October 31, 2024

Ruby shorts 2

In Ruby, everything has its "class"
Even the numbers. 1.class will give you Integer.

One more thing. You can make your own class with the "class" keyword. 

class YourClass
end

yours = YourClass.new # remember "new" here
yours.class

will return YourClass

You can even to this -- this is the fun part -- put a method in your class.

class YourClass
  def your_number
    1
  end
end

yours.your_number # returns 1

If you want to change that, if you want to return 42 instead, then you can write it one more time (with that different code)

class YourClass
  def your_number
    42
  end
end

yours.your_number # returns 42

See you next time!

About Kang-Kyu Lee

I love hamburgers. I will share my experiences with Ruby and YouTube