Created
June 10, 2014 21:19
-
-
Save Supernats/75623a89107632ab2f18 to your computer and use it in GitHub Desktop.
attr_accessor and def cannot team up
This file contains 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 AttrFirst | |
attr_reader :age | |
attr_accessor :name | |
def initialize(age = 0) | |
@age = age | |
end | |
def name | |
make_name(5) | |
end | |
def make_name(num_chars) | |
Array.new(num_chars).map { alphabet.sample }.join('') | |
end | |
def alphabet | |
("a".."z").to_a | |
end | |
end | |
class AttrLast | |
attr_reader :age | |
def initialize(age = 0) | |
@age = age | |
end | |
def name | |
make_name(5) | |
end | |
def make_name(num_chars) | |
Array.new(num_chars).map { alphabet.sample }.join('') | |
end | |
def alphabet | |
("a".."z").to_a | |
end | |
attr_accessor :name | |
end | |
f = AttrFirst.new | |
f.name # => "wiqwn" | |
l = AttrLast.new | |
l.name # => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment