call a getter on an instance of a class in ruby

In Ruby, you can define getter methods for instance variables using the attr_reader or attr_accessor methods.

Here is an example of how to call a getter on an instance of a class in Ruby:

main.rb
class Person
  attr_reader :name

  def initialize(name)
    @name = name
  end
end

person = Person.new("John")
puts person.name # "John"
139 chars
11 lines

In this example, the Person class has a getter method for the name instance variable defined using attr_reader, which allows us to access the value of name using the .name method on an instance of Person.

gistlibby LogSnag