G
Greg Willits
Can't see the tree for the forest.
Playing with how to integrate modules, classes, subclasses, and create
instance methods and class methods from modules.
In this play code below, I want to use initialize_colors in either the
class Shape or Square as a class method.
At this point I'm sure I'm missing a very simple syntax rule explained
very clearly in the books, but the code as is complains about
initialize_colors as being undefined, and I'm just not recognizing what
I am missing.
module Color
def initialize_colors
@fill_color = 'ffffff'
@border_color = '000000'
end
def set_fill_rgb(color)
if color == 'white'
@fill_color = 'ffffff'
elsif color == 'red'
@fill_color = 'ff0000'
elsif color == 'blue'
@fill_color = '0000ff'
elsif color == 'black'
@fill_color = '000000'
end
end
end
class Shape
include Color
attr_reader :fill_color, :border_color
def initialize
@fill_color = String.new
@border_color = String.new
end
end
class Square < Shape
initialize_colors
end
sq = Square.new
puts sq.fill_color
sq.set_fill_rgb('red')
puts sq.fill_color
Playing with how to integrate modules, classes, subclasses, and create
instance methods and class methods from modules.
In this play code below, I want to use initialize_colors in either the
class Shape or Square as a class method.
At this point I'm sure I'm missing a very simple syntax rule explained
very clearly in the books, but the code as is complains about
initialize_colors as being undefined, and I'm just not recognizing what
I am missing.
module Color
def initialize_colors
@fill_color = 'ffffff'
@border_color = '000000'
end
def set_fill_rgb(color)
if color == 'white'
@fill_color = 'ffffff'
elsif color == 'red'
@fill_color = 'ff0000'
elsif color == 'blue'
@fill_color = '0000ff'
elsif color == 'black'
@fill_color = '000000'
end
end
end
class Shape
include Color
attr_reader :fill_color, :border_color
def initialize
@fill_color = String.new
@border_color = String.new
end
end
class Square < Shape
initialize_colors
end
sq = Square.new
puts sq.fill_color
sq.set_fill_rgb('red')
puts sq.fill_color