Skip to content

Instantly share code, notes, and snippets.

@banyan
Created July 25, 2012 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banyan/3177143 to your computer and use it in GitHub Desktop.
Save banyan/3177143 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
describe Array do
let(:array) { [1,2,3,4,5] }
let(:words) { ['paperboy', 'lolipop', 'muumuu-domain', '30days album', 'sqale', 'osaipo', 'heteml'] }
it "配列の各要素を二倍した配列を返すこと" do
array.map{|n| n * 2 }.should == [2,4,6,8,10]
end
it "配列の要素をすべて足し算した結果を返すこと" do
array.inject{|sum, i| sum + i }.should == 15
array.inject(:+).should == 15
end
it "要素に連番を振ること" do
words = ['paperboy', 'lolipop', 'muumuu-domain', '30days album', 'sqale', 'osaipo', 'heteml']
words.each_with_index do |word, index|
p "#{index}: #{word}"
end
end
it "偶数の要素だけ返すこと" do
array.select{|n| n % 2 == 0 }.should == [2, 4]
end
it "nil 以外の要素だけを返すこと" do
a = [1, nil, 2, 3, nil, 4, 5]
a.compact.should == array
a.reject{|n| n.nil? }.should == array
a.select{|n| n.is_a? Fixnum }.should == array
end
it "最初の偶数を返すこと" do
array.find{|n| n % 2 == 0 }.should == 2
array.detect{|n| n % 2 == 0 }.should == 2
end
it "e を含む単語だけ返すこと" do
words.select{|word| word.match(/e/) }.should == ["paperboy", "sqale", "heteml"]
words.find_all{|word| word.match(/e/) }.should == ["paperboy", "sqale", "heteml"]
end
# 2番目の問いと同じ回答
it "配列の要素をすべて足し算した結果を返すこと" do
array.inject{|sum, i| sum + i }.should == 15
array.inject(:+).should == 15
end
it "要素に連番を振ること (二回目)" do
words.map.with_index{|word, index| "#{index}: #{word}" }.should == ["0: paperboy", "1: lolipop", "2: muumuu-domain", "3: 30days album", "4: sqale", "5: osaipo", "6: heteml"]
end
it "整形されたマークダウンを出力すること" do
words = ["perl", "ruby", "python", "css", "js", "html", "c", "c++", "java"]
puts words.each_slice(3).map {|chunked_words| chunked_words.map.with_index{|word, index| " #{index + 1}. #{word}"}.unshift("\n--") }
end
it "整形されたマークダウンを出力すること" do
words = ["perl", "ruby", "python", "css", "js", "html", "c", "c++", "java"]
puts words.each_with_index.inject([]) {|array, (word, index)|
array << "\n--" if index % 3 == 0
array << " #{(index % 3) + 1}. #{word}"
}
end
it "整形されたマークダウンを出力すること" do
words = ["perl", "ruby", "python", "css", "js", "html", "c", "c++", "java"]
words.shift(3).each_with_index {|word, index|
puts "\n--" if index % 3 == 0
puts " #{index + 1}. #{word}"
} until words.empty?
end
it "3で割り切れるものだけを足す" do
(1..30).select{|n| n % 3 == 0}.inject(:+).should == 165
end
end
Array
配列の各要素を二倍した配列を返すこと
配列の要素をすべて足し算した結果を返すこと
"0: paperboy"
"1: lolipop"
"2: muumuu-domain"
"3: 30days album"
"4: sqale"
"5: osaipo"
"6: heteml"
要素に連番を振ること
偶数の要素だけ返すこと
nil 以外の要素だけを返すこと
最初の偶数を返すこと
e を含む単語だけ返すこと
配列の要素をすべて足し算した結果を返すこと
要素に連番を振ること (二回目)
--
1. perl
2. ruby
3. python
--
1. css
2. js
3. html
--
1. c
2. c++
3. java
整形されたマークダウンを出力すること
--
1. perl
2. ruby
3. python
--
1. css
2. js
3. html
--
1. c
2. c++
3. java
整形されたマークダウンを出力すること
--
1. perl
2. ruby
3. python
--
1. css
2. js
3. html
--
1. c
2. c++
3. java
整形されたマークダウンを出力すること
3で割り切れるものだけを足す
Finished in 0.00366 seconds
13 examples, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment