This file contains hidden or 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
#http://jp.rubyist.net/magazine/?0021-Rspec#l28 | |
#Arrayクラスのテストコード(配列が空の場合) | |
array_spec.rb | |
describe Array, "when empty" do | |
before do | |
@empty_array = [] | |
@array = Array.new(3){ Hash.new } | |
@array[0][:cat] = "Nuko" | |
end |
This file contains hidden or 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
---repeat_joinメソッド | |
def repeat_join(array) | |
array.inject{ |a,b| a << "," << b } | |
end | |
---odd_sumメソッド | |
def odd_sum(array) | |
array.select{ |i| i % 2 == 1}.inject{ |a,b| a + b } |
This file contains hidden or 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
---自作ソース----- | |
def log(args) | |
p args + " before" | |
v = yield(args) | |
p args + " after" | |
v | |
end | |
---ここまで------ | |
def foo |
This file contains hidden or 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
def obj.foo(*args) | |
h = {:name=>"A", :no=>1} | |
last = args.pop | |
if last.is_a?(Hash) | |
last.each do|key,value| | |
h[key] = value | |
end | |
else | |
args << last | |
end |