Everything is object in Ruby

JiaHung Lin
Sep 10, 2017 · 3 min read

Object

class Student
def initialize(name, gender)
@name = name
@gender = gender
end
def name
@name
end
def age
@age
end

def say_hello
"#{@name} say 'Hello'"
end
end
student1 = Student.new("John Snow", "male")
student2 = Student.new("Daenerys Targaryen", "female")
student1.name #=> "Jon Snow"
student2.age #=> "female"
student2.say_hello #=> "Daenerys Targaryen say 'Hello'"

Class

How class create a object

new_student = Student.new("your name", "your gender")

Are Class and Module object?

"string".class
=> String
1.class
=> Fixnum
Class.class
=> Class
Module.class
=> Class

Superclass/subclass

String.ancestors
=> [String, Comparable, Object, Kernel, BasicObject]
Class.ancestors
=> [Class, Module, Object, Kernel, BasicObject]
Module.ancestors
=> [Module, Object, Kernel, BasicObject]
Fixnum.ancestors
=> [Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
BasicOject.class
=> class
Diagram from Skilldrick

JiaHung Lin

Written by

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade