Ruby で配列から特定の要素のみを抽出する場合

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

aiueo = ['', '', '', '', '']
auo   = aiueo.values_at(0, 2, 4)
auo.each do |word|
  puts word
end

実行結果は

あ
う
お

となる。

応用編

カンマ区切りで二重引用符のフィールドセパレーターの CSV 形式のデータを、タブ区切りのデータ形式に変換する。

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

lines = <<EOS
"2009-12-01","03-1234-5678","山田太郎"
"2010-01-13","012-234-5678","削除摺造"
"1999-06-01","090-1234-5678","山田次郎"
"1912-10-10","1234-56-7890","山田花子"
EOS

puts 'The value is string as multi lines.'
puts lines + "\n"

lines.split("\n").values_at(0, 2, 3).each do |line|
  puts line.split(',').map{|field| field.gsub(/^"|"$/, '')}.join("\t")
end

実行結果は

The value is string as multi lines.
"2009-12-01","03-1234-5678","山田太郎"
"2010-01-13","012-234-5678","削除摺造"
"1999-06-01","090-1234-5678","山田次郎"
"1912-10-10","1234-56-7890","山田花子"

2009-12-01        03-1234-5678    山田太郎
1999-06-01        090-1234-5678   山田次郎
1912-10-10        1234-56-7890    山田花子

となる。