Skip to content

Instantly share code, notes, and snippets.

@mdarby
Created April 11, 2011 21:48
Show Gist options
  • Select an option

  • Save mdarby/914436 to your computer and use it in GitHub Desktop.

Select an option

Save mdarby/914436 to your computer and use it in GitHub Desktop.
class Car
attr_accessor :vin_number
end
my_car = Car.new
my_car.to_s => "Car#<79860879878>"
# to_s is already defined via Object.to_s. Since Car inherits from Object, we get it for free.
# We can also redefine it to make it more useful too:
class Car
attr_accessor :vin_number
def to_s
vin_number
end
end
my_car = Car.new
my_car.vin_number = "123asdf"
my_car.to_s => "123asdf"
# We can also use the "my_car" variable in a String and it will implicitly call "to_s":
puts "#{my_car}" => "123asdf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment