Skip to content

Instantly share code, notes, and snippets.

@Supernats
Created June 10, 2014 21:40
Show Gist options
  • Save Supernats/e0bf2c9af412ca3dca92 to your computer and use it in GitHub Desktop.
Save Supernats/e0bf2c9af412ca3dca92 to your computer and use it in GitHub Desktop.
attr_accessor and def cannot team up
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