Skip to content

Instantly share code, notes, and snippets.

#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
---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 }
---自作ソース-----
def log(args)
p args + " before"
v = yield(args)
p args + " after"
v
end
---ここまで------
def foo
@itabatakafumi
itabatakafumi / RubyTraning
Created May 14, 2010 04:22
可変長引数と名前付き引数
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