第20回 Ruby 勉強会 @ 関西
行ってきた。
内容は置いといて、初級者レッスンの解答例(answer.pdf)に補足説明を入れておく。 資料は上記サイトでダウンロードしてくださいな。
以下では、配布資料(print.pdf)が手元にあることを前提に進めていきます。
実装するメソッド
- empty?
- スタックが空なら true、そうでなければ false を返す。
- size
- スタックのサイズを返す。
- push(val)
- 引数の値をスタックの一番上に積む。
- pop
- スタックの一番上の値を取り除いて返す。 スタックが空の場合は Stack::EmptyStackErrorが発生する。
テスト駆動開発版
TDD でやってくのです。
Step1
- Stack#empty? のテスト
- 「新しいスタックの empty? は真」
ということなのでまずはテストを書きます。
test_stack.rb
require 'stack'
require 'test/unit'
class TestStack < Test::Unit::TestCase
def setup
@stack = Stack.new
end
def test_empty?
assert(@stack.empty?, 'a new stack is empty.')
end
end
stack.rb
class Stack
def empty?
true
end
end
test_stack.rb
require 'stack'
require 'test/unit'
class TestStack < Test::Unit::TestCase
def setup
@stack = Stack.new
end
def test_empty?
assert(@stack.empty?, 'a new stack is empty.')
end
def test_push_and_pop
@stack.push(3)
assert_equal(3, @stack.pop, 'pop returns the last value.')
end
end
stack.rb
class Stack
def empty?
true
end
def push(val)
end
def pop
return 3
end
end
test_stack.rb
require 'stack'
require 'test/unit'
class TestStack < Test::Unit::TestCase
def setup
@stack = Stack.new
end
def test_empty?
assert(@stack.empty?, 'a new stack is empty.')
end
def test_push_and_pop
@stack.push(3)
assert_equal(3, @stack.pop, 'pop returns the last value.')
end
def test_push_and_size
@stack.push(3)
assert_equal(1, @stack.size, 'push increments the size.')
end
end
stack.rb
class Stack
def empty?
true
end
def push(val)
end
def pop
return 3
end
def size
1
end
end
test_stack.rb
require 'stack'
require 'test/unit'
class TestStack < Test::Unit::TestCase
def setup
@stack = Stack.new
end
def test_empty?
assert(@stack.empty?, 'a new stack is empty.')
end
def test_push_and_pop
@stack.push(3)
assert_equal(3, @stack.pop, 'pop returns the last value.')
end
def test_push_and_size
@stack.push(3)
assert_equal(1, @stack.size, 'push increments the size.')
@stack.push(5)
assert_equal(2, @stack.size, 'push increments the size.')
end
end
stack.rb
class Stack
def initialize
@size = 0
end
def empty?
true
end
def push(val)
@size += 1
end
def pop
return 3
end
def size
return @size
end
end
test_stack.rb
require 'stack'
require 'test/unit'
class TestStack < Test::Unit::TestCase
def setup
@stack = Stack.new
end
def test_empty?
assert(@stack.empty?, 'a new stack is empty.')
end
def test_push_and_pop
@stack.push(3)
assert_equal(3, @stack.pop, 'pop returns the last value.')
end
def test_push_and_size
@stack.push(3)
assert_equal(1, @stack.size, 'push increments the size.')
@stack.push(5)
assert_equal(2, @stack.size, 'push increments the size.')
end
def test_push_and_empty?
@stack.push(3)
assert_equal(false, @stack.empty?, 'a stack with data is not empty.')
end
end
stack.rb
class Stack
def initialize
@size = 0
end
def empty?
return @size == 0
end
def push(val)
@size += 1
end
def pop
return 3
end
def size
return @size
end
end
test_stack.rb
require 'stack'
require 'test/unit'
class TestStack < Test::Unit::TestCase
def setup
@stack = Stack.new
end
def test_empty?
assert(@stack.empty?, 'a new stack is empty.')
end
def test_push_and_pop
@stack.push(3)
assert_equal(3, @stack.pop, 'pop returns the last value.')
end
def test_push_and_size
@stack.push(3)
assert_equal(1, @stack.size, 'push increments the size.')
@stack.push(5)
assert_equal(2, @stack.size, 'push increments the size.')
end
def test_push_and_empty?
@stack.push(3)
assert_equal(false, @stack.empty?, 'a stack with data is not empty.')
end
def test_empty_pop
assert_raise(Stack::EmptyStackError,
'to pop a empty stack raise an error.') {@stack.pop}
end
end
stack.rb
class Stack
class EmptyStackError < StandardError; end
def initialize
@size = 0
end
def empty?
return @size == 0
end
def push(val)
@size += 1
end
def pop
raise EmptyStackError if empty?
return 3
end
def size
return @size
end
end
test_stack.rb
require 'stack'
require 'test/unit'
class TestStack < Test::Unit::TestCase
def setup
@stack = Stack.new
end
def test_empty?
assert(@stack.empty?, 'a new stack is empty.')
end
def test_push_and_pop
@stack.push(3)
assert_equal(3, @stack.pop, 'pop returns the last value.')
end
def test_push_and_size
@stack.push(3)
assert_equal(1, @stack.size, 'push increments the size.')
@stack.push(5)
assert_equal(2, @stack.size, 'push increments the size.')
end
def test_push_and_empty?
@stack.push(3)
assert_equal(false, @stack.empty?, 'a stack with data is not empty.')
end
def test_empty_pop
assert_raise(Stack::EmptyStackError,
'to pop a empty stack raise an error.') {@stack.pop}
end
def test_push_push_pop_and_size
@stack.push(3)
@stack.push(5)
@stack.pop
assert_equal(1, @stack.size, 'pop decrements the size.')
end
end
stack.rb
class Stack
class EmptyStackError < StandardError; end
def initialize
@size = 0
end
def empty?
return @size == 0
end
def push(val)
@size += 1
end
def pop
raise EmptyStackError if empty?
@size -= 1
return 3
end
def size
return @size
end
end
test_stack.rb
require 'stack'
require 'test/unit'
class TestStack < Test::Unit::TestCase
def setup
@stack = Stack.new
end
def test_empty?
assert(@stack.empty?, 'a new stack is empty.')
end
def test_push_and_pop
@stack.push(3)
assert_equal(3, @stack.pop, 'pop returns the last value.')
end
def test_push_and_size
@stack.push(3)
assert_equal(1, @stack.size, 'push increments the size.')
@stack.push(5)
assert_equal(2, @stack.size, 'push increments the size.')
end
def test_push_and_empty?
@stack.push(3)
assert_equal(false, @stack.empty?, 'a stack with data is not empty.')
end
def test_empty_pop
assert_raise(Stack::EmptyStackError,
'to pop a empty stack raise an error.') {@stack.pop}
end
def test_push_push_pop_and_size
@stack.push(3)
@stack.push(5)
@stack.pop
assert_equal(1, @stack.size, 'pop decrements the size.')
end
def test_push_push_and_pop
@stack.push(3)
@stack.push(5)
assert_equal(5, @stack.pop, 'pop returns the last value.')
end
end
stack.rb
class Stack
class EmptyStackError < StandardError; end
def initialize
@size = 0
@values = Array.new
end
def empty?
return @size == 0
end
def push(val)
@size += 1
@values[@size - 1] = val
end
def pop
raise EmptyStackError if empty?
val = @values[@size - 1]
@size -= 1
return val
end
def size
return @size
end
end
irb(main):001:0> load 'stack.rb'
=> true
irb(main):002:0> s = Stack.new
=> #
irb(main):003:0> s.push 1
=> 1
irb(main):004:0> s.push 2
=> 2
irb(main):005:0> s.push 3
=> 3
irb(main):006:0> s.inspect
=> "#"
irb(main):007:0> s.pop
=> 3
irb(main):008:0> s.pop
=> 2
irb(main):009:0> s.pop
=> 1
irb(main):010:0> s.inspect
=> "#"
irb(main):011:0>
@values の中身が Stack#pop しても消えませんね。これはスタックとしては拙いのでリファクタリングしましょう。
というわけで次回に続く。
Posted in Ruby | コメントはありません | atom
Trackbacks
Use the following link to trackback from your own site:
http://typo.okkez.net/trackbacks?article_id=ruby-kansaiworkshop-20th&day=02&month=11&year=2007