Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d40c660
commit 72889de
Showing
22 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'rake/testtask' | ||
|
||
Rake::TestTask.new do |t| | ||
t.pattern = 'test/**/*_test.rb' | ||
end | ||
|
||
task default: :test |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require './lib/deep_freezable' | ||
|
||
class Bank | ||
extend DeepFreezable | ||
|
||
CURRENCIES = deep_freeze({ 'Japan' => 'yen', 'US' => 'dollar', 'India' => 'rupee' }) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def convert_hash_syntax(old_syntax) | ||
old_syntax.gsub(/:(\w+) *=> */, '\1: ') | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
UNITS = { m: 1.0, ft: 3.28, in: 39.37 } | ||
def convert_length(length, from: :m, to: :m) | ||
(length / UNITS[from] * UNITS[to]).round(2) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module DeepFreezable | ||
def deep_freeze(array_or_hash) | ||
case array_or_hash | ||
when Array | ||
array_or_hash.each do |element| | ||
element.freeze | ||
end | ||
when Hash | ||
array_or_hash.each do |key, value| | ||
key.freeze | ||
value.freeze | ||
end | ||
end | ||
array_or_hash.freeze | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Effects | ||
def self.reverse | ||
->(words) do | ||
words.split(' ').map(&:reverse).join(' ') | ||
end | ||
end | ||
|
||
def self.echo(rate) | ||
->(words) do | ||
words.chars.map { |c| c == ' ' ? c : c * rate }.join | ||
end | ||
end | ||
|
||
def self.loud(level) | ||
->(words) do | ||
words.split(' ').map { |word| word.upcase + '!' * level }.join(' ') | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def fizz_buzz(n) | ||
if n % 15 == 0 | ||
'Fizz Buzz' | ||
elsif n % 3 == 0 | ||
'Fizz' | ||
elsif n % 5 == 0 | ||
'Buzz' | ||
else | ||
n.to_s | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
def fizz_buzz(n) | ||
if n % 15 == 0 | ||
'Fizz Buzz' | ||
elsif n % 3 == 0 | ||
'Fizz' | ||
elsif n % 5 == 0 | ||
'Buzz' | ||
else | ||
n.to_s | ||
end | ||
end | ||
|
||
puts fizz_buzz(1) | ||
puts fizz_buzz(2) | ||
puts fizz_buzz(3) | ||
puts fizz_buzz(4) | ||
puts fizz_buzz(5) | ||
puts fizz_buzz(6) | ||
puts fizz_buzz(15) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 改札機を表すクラス | ||
class Gate | ||
STATIONS = [:umeda, :juso, :mikuni] # :nodoc: | ||
FARES = [150, 190] # :nodoc: | ||
|
||
# Gateオブジェクトの作成 | ||
# ==== 引数 | ||
# * +name+ - 駅名 | ||
def initialize(name) | ||
@name = name | ||
end | ||
|
||
# 改札機を通って駅に入場する | ||
# ==== 引数 | ||
# * +ticket+ - 切符 | ||
def enter(ticket) | ||
ticket.stamp(@name) | ||
end | ||
|
||
# 改札機を通って駅から出場する | ||
# ==== 引数 | ||
# * +ticket+ - 切符 | ||
# ==== 戻り値 | ||
# * +boolean+ - 運賃が足りていれば+true+、不足していれば+false+ | ||
def exit(ticket) | ||
fare = calc_fare(ticket) | ||
fare <= ticket.fare | ||
end | ||
|
||
private | ||
|
||
def calc_fare(ticket) | ||
from = STATIONS.index(ticket.stamped_at) | ||
to = STATIONS.index(@name) | ||
distance = to - from | ||
FARES[distance - 1] | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
print 'Text?: ' | ||
text = gets.chomp | ||
|
||
begin | ||
print 'Pattern?: ' | ||
pattern = gets.chomp | ||
regexp = Regexp.new(pattern) | ||
rescue RegexpError => e | ||
puts "Invalid pattern: #{e.message}" | ||
retry | ||
end | ||
|
||
matches = text.scan(regexp) | ||
if matches.size > 0 | ||
puts "Matched: #{matches.join(', ')}" | ||
else | ||
puts "Nothing matched." | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
def to_hex(r, g, b) | ||
[r, g, b].inject('#') do |hex, n| | ||
hex + n.to_s(16).rjust(2, '0') | ||
end | ||
end | ||
|
||
def to_ints(hex) | ||
hex.scan(/\w\w/).map(&:hex) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require './lib/deep_freezable' | ||
|
||
class Team | ||
extend DeepFreezable | ||
|
||
COUNTRIES = deep_freeze(['Japan', 'US', 'India']) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Ticket | ||
attr_reader :fare, :stamped_at | ||
|
||
def initialize(fare) | ||
@fare = fare | ||
end | ||
|
||
def stamp(name) | ||
@stamped_at = name | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class WordSynth | ||
def initialize | ||
@effects = [] | ||
end | ||
|
||
def add_effect(effect) | ||
@effects << effect | ||
end | ||
|
||
def play(original_words) | ||
@effects.inject(original_words) do |words, effect| | ||
effect.call(words) | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'minitest/autorun' | ||
require './lib/convert_hash_syntax' | ||
|
||
class ConvertHashSyntaxTest < Minitest::Test | ||
def test_convert_hash_syntax | ||
old_syntax = <<~TEXT | ||
{ | ||
:name => 'Alice', | ||
:age=>20, | ||
:gender => :female | ||
} | ||
TEXT | ||
expected = <<~TEXT | ||
{ | ||
name: 'Alice', | ||
age: 20, | ||
gender: :female | ||
} | ||
TEXT | ||
assert_equal expected, convert_hash_syntax(old_syntax) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'minitest/autorun' | ||
require './lib/convert_length' | ||
|
||
class ConvertLengthTest < Minitest::Test | ||
def test_convert_length | ||
assert_equal 39.37, convert_length(1, from: :m, to: :in) | ||
assert_equal 0.38, convert_length(15, from: :in, to: :m) | ||
assert_equal 10670.73, convert_length(35000, from: :ft, to: :m) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'minitest/autorun' | ||
require './lib/bank' | ||
require './lib/team' | ||
|
||
class DeepFreezableTest < Minitest::Test | ||
def test_deep_freeze_to_array | ||
# 配列の値は正しいか? | ||
assert_equal ['Japan', 'US', 'India'], Team::COUNTRIES | ||
# 配列自身がfreezeされているか? | ||
assert Team::COUNTRIES.frozen? | ||
# 配列の要素がすべてfreezeされているか? | ||
assert Team::COUNTRIES.all? { |country| country.frozen? } | ||
end | ||
|
||
def test_deep_freeze_to_hash | ||
# ハッシュの値は正しいか? | ||
assert_equal( | ||
{ 'Japan' => 'yen', 'US' => 'dollar', 'India' => 'rupee' }, | ||
Bank::CURRENCIES | ||
) | ||
# ハッシュ自身がfreezeされているか? | ||
assert Bank::CURRENCIES.frozen? | ||
# ハッシュの要素(キーと値)がすべてfreezeされているか? | ||
assert Bank::CURRENCIES.all? { |key, value| key.frozen? && value.frozen? } | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'minitest/autorun' | ||
require './lib/effects' | ||
|
||
class EffectsTest < Minitest::Test | ||
def test_reverse | ||
effect = Effects.reverse | ||
assert_equal 'ybuR si !nuf', effect.call('Ruby is fun!') | ||
end | ||
|
||
def test_echo | ||
effect = Effects.echo(2) | ||
assert_equal 'RRuubbyy iiss ffuunn!!', effect.call('Ruby is fun!') | ||
|
||
effect = Effects.echo(3) | ||
assert_equal 'RRRuuubbbyyy iiisss fffuuunnn!!!', effect.call('Ruby is fun!') | ||
end | ||
|
||
def test_loud | ||
effect = Effects.loud(2) | ||
assert_equal 'RUBY!! IS!! FUN!!!', effect.call('Ruby is fun!') | ||
|
||
effect = Effects.loud(3) | ||
assert_equal 'RUBY!!! IS!!! FUN!!!!', effect.call('Ruby is fun!') | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'minitest/autorun' | ||
require './lib/fizz_buzz' | ||
|
||
class FizzBuzzTest < Minitest::Test | ||
def test_fizz_buzz | ||
assert_equal '1', fizz_buzz(1) | ||
assert_equal '2', fizz_buzz(2) | ||
assert_equal 'Fizz', fizz_buzz(3) | ||
assert_equal '4', fizz_buzz(4) | ||
assert_equal 'Buzz', fizz_buzz(5) | ||
assert_equal 'Fizz', fizz_buzz(6) | ||
assert_equal 'Fizz Buzz', fizz_buzz(15) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'minitest/autorun' | ||
require './lib/gate' | ||
require './lib/ticket' | ||
|
||
class GateTest < Minitest::Test | ||
def setup | ||
@umeda = Gate.new(:umeda) | ||
@juso = Gate.new(:juso) | ||
@mikuni = Gate.new(:mikuni) | ||
end | ||
|
||
def test_umeda_to_juso | ||
ticket = Ticket.new(150) | ||
@umeda.enter(ticket) | ||
assert @juso.exit(ticket) | ||
end | ||
|
||
def test_umeda_to_mikuni_when_fare_is_not_enough | ||
ticket = Ticket.new(150) | ||
@umeda.enter(ticket) | ||
refute @mikuni.exit(ticket) | ||
end | ||
|
||
def test_umeda_to_mikuni_when_fare_is_enough | ||
ticket = Ticket.new(190) | ||
@umeda.enter(ticket) | ||
assert @mikuni.exit(ticket) | ||
end | ||
|
||
def test_juso_to_mikuni | ||
ticket = Ticket.new(150) | ||
@juso.enter(ticket) | ||
assert @mikuni.exit(ticket) | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'minitest/autorun' | ||
require './lib/rgb' | ||
|
||
class RgbTest < Minitest::Test | ||
def test_to_hex | ||
assert_equal '#000000', to_hex(0, 0, 0) | ||
assert_equal '#ffffff', to_hex(255, 255, 255) | ||
assert_equal '#043c78', to_hex(4, 60, 120) | ||
end | ||
|
||
def test_to_ints | ||
assert_equal [0, 0, 0], to_ints('#000000') | ||
assert_equal [255, 255, 255], to_ints('#ffffff') | ||
assert_equal [4, 60, 120], to_ints('#043c78') | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'minitest/autorun' | ||
require './lib/word_synth' | ||
require './lib/effects' | ||
|
||
class WordSynthTest < Minitest::Test | ||
def test_play_without_effects | ||
synth = WordSynth.new | ||
assert_equal 'Ruby is fun!', synth.play('Ruby is fun!') | ||
end | ||
|
||
def test_play_with_reverse | ||
synth = WordSynth.new | ||
synth.add_effect(Effects.reverse) | ||
assert_equal 'ybuR si !nuf', synth.play('Ruby is fun!') | ||
end | ||
|
||
def test_play_with_many_effects | ||
synth = WordSynth.new | ||
synth.add_effect(Effects.echo(2)) | ||
synth.add_effect(Effects.loud(3)) | ||
synth.add_effect(Effects.reverse) | ||
assert_equal '!!!YYBBUURR !!!SSII !!!!!NNUUFF', synth.play('Ruby is fun!') | ||
end | ||
end |