Created
November 7, 2015 19:52
-
-
Save kkchu791/3a606281986297803910 to your computer and use it in GitHub Desktop.
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 Charlie | |
def initialize(guitar) | |
@guitar = guitar | |
end | |
def play(guitar) | |
guitar.type == 'electric' ? guitar.sound.upcase : guitar.sound.downcase | |
end | |
end | |
class Guitar | |
attr_accessor :type | |
def initialize(manufacturer, color, type="electric") | |
@manufacturer, @color, @type = manufacturer, color, type | |
end | |
def sound | |
'twang' | |
end | |
end | |
g = Guitar.new('Gibson', 'blue') | |
c = Charlie.new(g) | |
p c.play(g) | |
#Method sound would make more sense to be in class Guitar because it's a guitar action. | |
#Added guitar to sound.upcase and sound.downcase so it could find the method in class Guitar. | |
#Attr_accessor :guitar is unnecessary because the guitar attributes were set properly with def initialize(guitar) | |
#Attr_accessor :manufacturer, :color are unnecssary because the attributes are not called from within class Charlie. The type attribute is called from class Charlie so it is needed. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
But again, I feel that it is probably better to do as you suggest, and simply use the @guitar instance variable inside the Charlie class, as there is no need to access a guitar attribute from outside that class.