M
Marc Chanliau
In Ruby it seems you can instantiate a class inside the class itself or
outside. What is the accepted convention?
In the following example, instantiation is inside the class:
class Person
attr_accessor :name, :age, :gender
def initialize(name, age, gender)
@name = name
@age = age.to_i
@gender = gender
end
# method to print a new person
def printIt
print " "
print name
print ", "
print age
print ", "
print gender
puts
end
# generating instances and printing them from inside the class itself
person_instance = Person.new("Jessica", 17, "female")
person_instance.printIt
person_instance = Person.new("Marc", 60, "male")
person_instance.printIt
person_instance = Person.new("Linda", 56, "female")
person_instance.printIt
end
outside. What is the accepted convention?
In the following example, instantiation is inside the class:
class Person
attr_accessor :name, :age, :gender
def initialize(name, age, gender)
@name = name
@age = age.to_i
@gender = gender
end
# method to print a new person
def printIt
print " "
print name
print ", "
print age
print ", "
print gender
puts
end
# generating instances and printing them from inside the class itself
person_instance = Person.new("Jessica", 17, "female")
person_instance.printIt
person_instance = Person.new("Marc", 60, "male")
person_instance.printIt
person_instance = Person.new("Linda", 56, "female")
person_instance.printIt
end