Created
October 26, 2016 15:16
-
-
Save FilBot3/71907bb0f1a8d4e231e382ca080d5358 to your computer and use it in GitHub Desktop.
Ruby's Accessor Methods
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
| #!/usr/bin/env ruby | |
| # | |
| # RubyDocs: http://ruby-doc.org/core-2.3.1/Module.html#method-i-attr_accessor | |
| # | |
| module RubyLearning | |
| class AttrAccessor | |
| attr_accessor :name | |
| end | |
| class AttrReader | |
| attr_reader :name | |
| def initialize | |
| @name = "Jim Bob Joe Pants" | |
| end | |
| end | |
| class AttrWriter | |
| attr_writer :name | |
| def initialize | |
| @name = "Pants Joe Bob Jim" | |
| end | |
| end | |
| class ManualAttrAccessor | |
| def intialize | |
| @name = "Manuel" | |
| end | |
| def name | |
| @name | |
| end | |
| def name=(some_name) | |
| @name = some_name | |
| end | |
| end | |
| class ManualAttrReader | |
| def intialize | |
| @name = "Jerry" | |
| end | |
| def name | |
| @name | |
| end | |
| end | |
| class ManualAttrWriter | |
| def intialize | |
| @name = "Zatchary" | |
| end | |
| def name=(some_name) | |
| @name = some_name | |
| end | |
| end | |
| end | |
| example1 = RubyLearning::AttrAccessor.new | |
| example1.name = "Samuel" | |
| puts example1.name | |
| example2 = RubyLearning::AttrReader.new | |
| puts example2.name | |
| example3 = RubyLearning::AttrWriter.new | |
| example3.name = "Fran" | |
| example4 = RubyLearning::ManualAttrAccessor.new | |
| example4.name = "Greg" | |
| puts example4.name | |
| example5 = RubyLearning::ManualAttrReader.new | |
| puts example5.name | |
| example6 = RubyLearning::ManualAttrWriter.new | |
| example6.name = "Sven" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment