Created
April 11, 2011 21:48
-
-
Save mdarby/914436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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