class Command
attr_reader :name
def initialize(name)
@name = name
end
def run
raise "Implement me"
end
end
class MakeBold < Command
def initialize(text)
super("Make bold text: #{text}")
@text = text
end
def run; puts "<b>#{@text}</b>"; end
end
class MakeItalic < Command
def initialize(text)
super("Make italic text: #{text}")
@text = text
end
def run; puts "<i>#{@text}</i>"; end
end
class MakeSized < Command
def initialize(text, size=14)
super("Make text: #{text} to be size: #{size}")
@text = text
@size = size
end
def run; puts "<span style='font-size:#{@size}px;'>#{@text}</span>"; end
end
commands = []
commands << MakeBold.new("to be bold")
commands << MakeItalic.new("to be italic")
commands << MakeSized.new("to be sized", 20)
commands << MakeBold.new("other to be bold")
# show description for each command prior to execution
commands.each {|command| command.name }
# execute commands
commands.each {|command| command.run }
Output:
$ ruby command.rb
<b>to be bold</b>
<i>to be italic</i>
<span style='font-size:20px;'>to be sized</span>
<b>other to be bold</b>
No comments:
Post a Comment