Methods and instance variables

    Right now, we can’t do much with a person aside from create it with a name. Its age will always be zero. So lets add a method that makes a person become older:

    1. def initialize(@name : String)
    2. @age = 0
    3. end
    4. def age
    5. @age
    6. end
    7. def become_older
    8. @age += 1
    9. end
    10. end
    11. peter = Person.new "Peter"
    12. john.age # => 0
    13. john.age # => 1
    14. peter.age # => 0

    Method names begin with a lowercase letter and, as a convention, only use lowercase letters, underscores and numbers.

    For more information on getter and setter macros, see the standard library documentation for Object#getter, , and Object#property.

    As a side note, we can define become_older inside the original Person definition, or in a separate definition: Crystal combines all definitions into a single class. The following works just fine:

    1. class Person
    2. def initialize(@name : String)
    3. @age = 0
    4. end
    5. end
    6. class Person
    7. def become_older
    8. @age += 1
    9. end

    You can invoke the previously redefined method with previous_def:

    1. class Person
    2. @age += 1
    3. end
    4. end
    5. class Person
    6. def become_older
    7. previous_def
    8. @age += 2
    9. end
    10. end
    11. person = Person.new "John"
    12. person.become_older
    13. person.age # => 3

    Without arguments or parentheses, previous_def receives all of the method’s parameters as arguments. Otherwise, it receives the arguments you pass to it.

    This will initialize @age to zero in every constructor. This is useful to avoid duplication, but also to avoid the Nil type when reopening a class and adding instance variables to it.