"class methods" are actually
def Person.oldest(people)
people.sort_by {|p| p.age}.last
end
class Person
def self.oldest(people)
people.sort_by {|p| p.age}.last
end
end
self
points to the class inside a class definition... def self.oldest
is the same as def Person.oldest
By the way, here's an even cooler way to implement that method:
people.max_by(:&age)
class Person
def self.from_string(s)
tokens = s.split
new(tokens[0], tokens[1], tokens[2].to_i)
end
def initialize(first, last, age)
@first, @last, @age = first, last, age
end
end
>> alice = Person.new("Alice", "Andrews", 17)
=> #<Person:0x000001009eca90
@first="Alice", @last="Andrews", @age=17>
>> bob = Person.from_string("Bob Barker 67")
=> #<Person:0x000001009e3af8
@first="Bob", @last="Barker", @age=67>
class Person
@count = 0 # instance variable of the class Person
def self.one_more
@count += 1
end
def self.count
@count
end
def initialize(first, last, age)
@first, @last, @age = first, last, age
Person.one_more
end
end
By convention, constants who are values are in ALL_CAPS
, and constants who are modules or classes are in CamelCase
class Person
DEFAULT_AGE = 18
# inside the class, can be named directly
def initialize(first, last, age = DEFAULT_AGE)
@first, @last, @age = first, last, age
end
end
Person::DEFAULT_AGE #=> 18 ```
Math::PI
RUBY_VERSION
RUBY_RELEASE_DATE
/