Skip to content

Instantly share code, notes, and snippets.

@2get
Created July 24, 2012 03:19
Show Gist options
  • Save 2get/3167815 to your computer and use it in GitHub Desktop.
Save 2get/3167815 to your computer and use it in GitHub Desktop.
Array methods quiz
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# $ rspec array_quest_spec.rb
class ArrayQuest
def q1(array)
array.first
end
def q2(array)
array.last
end
def q3(array, n)
array.fetch(n)
end
def q3_ex1(array, n)
array.at(n)
end
def q3_ex2(array, n)
array[n] if array.length > n - 1
end
def q4(array)
array.empty?
end
def q5(array)
array.sample
end
def q5_ex1(array)
array[rand(array.length)]
end
def q6(array)
array.shuffle
end
def q6_ex1(array)
array.sort_by{ rand }
end
def q7(array)
array.clone
end
def q7_ex1(array)
array.dup
end
def q7_ex2(array)
Marshal.load(Marshal.dump(array))
end
def q8(array)
array.clear
end
def q8_ex1(array)
array.length.times do
array.pop
end
array
end
def q9(array, value)
array.push(value)
end
def q9_ex1(array, value)
array << value
end
def q10(array)
array[0..-2]
end
def q10(array)
array.pop(1)
array
end
def q10_ex1(array)
array.delete_at(array.length - 1)
array
end
def q10_ex2(array)
array.first(array.length - 1)
end
def q11(array, value)
array.insert(0, value)
end
def q11_ex1(array, value)
array.unshift(value)
end
def q12(array)
array[1..-1]
end
def q12_ex1(array)
array.shift
array
end
def q12_ex2(array)
array.delete_at(0)
array
end
def q12_ex3(array)
array.last(array.length - 1)
end
def q13(array, str)
array.join(str)
end
def q13_ex1(array, str)
ret = ''
array.each do |v|
ret << v.to_s
ret << str
end
ret.slice(0, ret.length - 1)
end
def q14(array)
array.insert(3, "ruby")
end
def q15(array)
array.sort
end
def q16(array)
array.reverse
end
end
describe "ArrayQuest" do
let(:e) { [1, 2, 3, 4, 5] }
before do
@aq = ArrayQuest.new
end
describe "#q1" do
it "は先頭の要素(1)を返す" do
@aq.q1(e).should == 1
end
end
describe "#q2" do
it "は末尾の要素(5)を返す" do
@aq.q2(e).should == 5
end
end
describe "#q3" do
it "は配列のn番目の要素を返す" do
@aq.q3(e, 4).should == 5
end
it "#q3_ex1は配列のn番目の要素を返す" do
@aq.q3_ex1(e, 4).should == 5
end
it "#q3_ex2は配列のn番目の要素を返す" do
@aq.q3_ex1(e, 4).should == 5
end
end
describe "#q4" do
it "は配列が空ならtrueを返す" do
@aq.q4([]).should be_true
end
it "は配列が空でないときfalseを返す" do
@aq.q4(e).should be_false
end
end
describe "#q5" do
pending "は要素をランダムに一個返す | ランダムがわからないのでpending" do
@aq.q5(e).should == 1
end
pending "#q5_ex1は要素をランダムに一個返す | ランダムがわからないのでpending" do
@aq.q5_ex1(e).should == 1
end
end
describe "#q6" do
pending "は全ての要素をランダムに入れ替えて返す | ランダムがわからないのでpending" do
@aq.q6(e).should == 1
end
pending "#q6_ex1は全ての要素をランダムに入れ替えて返す | ランダムがわからないのでpending" do
@aq.q6_ex1(e).should == 1
end
end
describe "#q7" do
it "は配列を複製して返す" do
@aq.q7(e).should == [1, 2, 3, 4, 5]
end
it "返り値はオブジェクトIDが違う" do
@aq.q7(e).object_id.should_not == e.object_id
end
it "#q7_ex1は配列を複製して返す" do
@aq.q7_ex1(e).should == [1, 2, 3, 4, 5]
end
it "#q7_ex1の返り値はオブジェクトIDが違う" do
@aq.q7_ex1(e).object_id.should_not == e.object_id
end
it "#q7_ex2は配列を複製して返す" do
@aq.q7_ex2(e).should == [1, 2, 3, 4, 5]
end
it "#q7_ex2の返り値はオブジェクトIDが違う" do
@aq.q7_ex2(e).object_id.should_not == e.object_id
end
end
describe "#q8" do
it "は配列を空にして返す" do
@aq.q8(e).should == []
end
it "#q8_ex1は配列を空にして返す" do
@aq.q8_ex1(e).should == []
end
end
describe "#q9" do
it "は末尾に要素(6)を追加する" do
@aq.q9(e, 6).should == [1, 2, 3, 4, 5, 6]
end
it "#q9_ex1は末尾に要素(6)を追加する" do
@aq.q9_ex1(e, 6).should == [1, 2, 3, 4, 5, 6]
end
end
describe "#q10" do
it "は末尾の要素(5)を削除する" do
@aq.q10(e).should == [1, 2, 3, 4]
end
it "#q10_ex1は末尾の要素(5)を削除する" do
@aq.q10_ex1(e).should == [1, 2, 3, 4]
end
it "#q10_ex2は末尾の要素(5)を削除する" do
@aq.q10_ex2(e).should == [1, 2, 3, 4]
end
end
describe "#q11" do
it "は先頭に要素(0)を追加する" do
@aq.q11(e, 0).should == [0, 1, 2, 3, 4, 5]
end
it "#q11_ex1は先頭に要素(0)を追加する" do
@aq.q11_ex1(e, 0).should == [0, 1, 2, 3, 4, 5]
end
end
describe "#q12" do
it "は先頭の要素(1)を削除する" do
@aq.q12(e).should == [2, 3, 4, 5]
end
it "#q12_ex1は先頭の要素(1)を削除する" do
@aq.q12_ex1(e).should == [2, 3, 4, 5]
end
it "#q12_ex2は先頭の要素(1)を削除する" do
@aq.q12_ex2(e).should == [2, 3, 4, 5]
end
it "#q12_ex3は先頭の要素(1)を削除する" do
@aq.q12_ex3(e).should == [2, 3, 4, 5]
end
end
describe "#q13" do
it "は[1,2,3,4,5] を 文字列の \"1-2-3-4-5\" にする" do
@aq.q13(e, '-').should == "1-2-3-4-5"
end
it "#q13_ex1は[1,2,3,4,5] を 文字列の \"1-2-3-4-5\" にする" do
@aq.q13_ex1(e, '-').should == "1-2-3-4-5"
end
end
describe "#q14" do
it "は[1,2,3,4,5] を [1,2,3,\"ruby\",4,5] にする" do
@aq.q14(e).should == [1, 2, 3, "ruby", 4, 5]
end
end
describe "#q15" do
it "は[1,3,2,5,4] を [1,2,3,4,5] にする" do
@aq.q15([1, 3, 2, 5, 4]).should == [1, 2, 3, 4, 5]
end
end
describe "#q16" do
it "は[1,2,3,4,5] を [5,4,3,2,1] にする" do
@aq.q16(e).should == [5, 4, 3, 2, 1]
end
end
end
Rspec.configure do |config|
# Use color is STDOUT
config.color_enabled = true
# Use the specified formatter
config.formatter = :documentation
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment