mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-24 15:51:41 +03:00
commit
900f5448eb
@ -275,36 +275,36 @@ surround { puts 'hello world' }
|
|||||||
# Define a class with the class keyword
|
# Define a class with the class keyword
|
||||||
class Human
|
class Human
|
||||||
|
|
||||||
# A class variable. It is shared by all instances of this class.
|
# A class variable. It is shared by all instances of this class.
|
||||||
@@species = "H. sapiens"
|
@@species = "H. sapiens"
|
||||||
|
|
||||||
# Basic initializer
|
# Basic initializer
|
||||||
def initialize(name, age=0)
|
def initialize(name, age=0)
|
||||||
# Assign the argument to the "name" instance variable for the instance
|
# Assign the argument to the "name" instance variable for the instance
|
||||||
@name = name
|
@name = name
|
||||||
# If no age given, we will fall back to the default in the arguments list.
|
# If no age given, we will fall back to the default in the arguments list.
|
||||||
@age = age
|
@age = age
|
||||||
end
|
end
|
||||||
|
|
||||||
# Basic setter method
|
# Basic setter method
|
||||||
def name=(name)
|
def name=(name)
|
||||||
@name = name
|
@name = name
|
||||||
end
|
end
|
||||||
|
|
||||||
# Basic getter method
|
# Basic getter method
|
||||||
def name
|
def name
|
||||||
@name
|
@name
|
||||||
end
|
end
|
||||||
|
|
||||||
# A class method uses self to distinguish from instance methods.
|
# A class method uses self to distinguish from instance methods.
|
||||||
# It can only be called on the class, not an instance.
|
# It can only be called on the class, not an instance.
|
||||||
def self.say(msg)
|
def self.say(msg)
|
||||||
puts "#{msg}"
|
puts "#{msg}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def species
|
def species
|
||||||
@@species
|
@@species
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user