開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Ruby 2.3 (プログラミング言語)
Head First Ruby (Jay McGavren (著)、O'Reilly Media)のChapter 4.(Initializing Instances: Off to a Great Start)、Using optional parameters with "initialize"、POOL PUZZLE(No. 2412)を取り組んでみる。
POOL PUZZLE(No. 2412)
コード(Emacs)
#!/usr/bin/env ruby2.3
# -*- coding: utf-8 -*-
class Car
def initialize(engine)
@engine = engine
end
def rev_engine
@engine.make_sound
end
end
class Engine
def initialize(sound = "Vroom!!")
@sound = sound
end
def make_sound
puts @sound
end
end
engine = Engine.new
car = Car.new(engine)
car.rev_engine
入出力結果(Terminal)
$ ./pool_puzzle.rb Vroom!! $
0 コメント:
コメントを投稿