RubyでHashを扱うまとめ

| コメント(0) | トラックバック(0)

Hashの扱いのまとめメモ公開。

# hash作成
   {:one=> 1, :two =>2}


# 要素の取得
   h[:one]


#設定 取得
   default     デフォルト値設定
   freeze      破壊methodから守る


# ループ
   each        key valueでループ
   each_key    keyでループ
   each_value  valueでループ


# データの存在チェック
   key?
   value?


# 削除
   delete
   delete_if   block評価で削除
   reject      削除対象がなければnil


# keyとvalueの入れ替え
   invert


# key valueの一覧
   keys
   valuse
   to_a


# hash結合
   merge
ruby
# -*- coding: utf-8 -*-

# hash作成
#
{:one => 1, :two => 2, :three => 3}

# ruby 1.9 から : で書ける
{one: 1, two: 2, three:  3}

# 要素の取得
#
h = {:one => 1, :two => 2, :three => 3}
h[:one]          #=> 1



# hashのdefault 設定 取得
#
h = Hash.new(0)

h[:not_exist]     #=> 0

h.default         #=> 0
h.default = 1     #=> 1
h[:not_exist]     #=> 1


# freeze  defaultを破壊methodから守る
#
h_df = Hash.new("default")
h_fz = Hash.new("default".freeze)

h_df[:not_exist]            #=> "default"
h_fz[:not_exist]            #=> "default"
h_df[:not_exist].upcase!    #=> "DEFAULT"
h_fz[:not_exist].upcase! rescue   $!
    #<RuntimeError: can't modify frozen string>


# hash のdefault をblockで指定
#
h = Hash.new {|j,k| j[k] = "defalut" }

h[:hoge]         #=> "defalut"
h[:fuga]         #=> "defalut"

# 破壊methodも対応
h[:moga].upcase! #=> "DEFALUT"
h
   #=> {:hoge=>"defalut", :fuga=>"defalut", :moga=>"DEFALUT"}




# ループ
#
# each       key valueでループ
# each_key   keyでループ
# each_value valueでループ
h = {:one => 1, :two => 2, :three => 3}
h.each do |key,value|
  puts "#{key} => #{value}"
end
   #=> one => 1
   #=> two => 2
   #=> three => 3

h = {:one => 1, :two => 2, :three => 3}
h.each_key do |key|
  puts "#{key}"
end
   #=> one
   #=> two
   #=> three

h = {:one => 1, :two => 2, :three => 3}
h.each_value do |value|
  puts "#{value}"
end
   #=> 1
   #=> 2
   #=> 3


# データの存在チェック
#
# key?
# value?

h = {:one => 1, :two => 2, :three => 3}
h.key? :four  #=> false
h.key? :one   #=> true


h = {:one => 1, :two => 2, :three => 3}
h.value? 1  #=> true
h.value? 4  #=> false


# 削除
#
# delete
# delete_if block評価で削除
# reject 削除対象がなければnil

h = {:one => 1, :two => 2, :three => 3}
h.delete(:one)  #=> 1
h               #=> {:two=>2, :three=>3}

# keyがない場合 nil
h.delete(:one)  #=> nil

# keyがない場合 blockを返す
h.delete(:one) { |key| "key not found :#{key}" }
    #=> "key not found :one"


h = {:one => 1, :two => 2, :three => 3}
h.delete_if { |key,value| key == :one && value == 1}
   #=> {:two=>2, :three=>3}
p h.delete_if { |key,value| key == :four && value == 4}
   #=> {:two=>2, :three=>3}

h  #=> {:three=>3}


h = {:one => 1, :two => 2, :three => 3}
p h.reject! { |key,value| key == :one && value == 1}
   #=> {:two=>2, :three=>3}
p h.reject! { |key,value| key == :four && value == 4}
   #=> nil
p h  #=> {:two=>2, :three=>3}



# invert keyとvalueの入れ替え
#
h = {:one => 1, :two => 2, :three => 3}
h.invert   #=> {1=>:one, 2=>:two, 3=>:three}


# key valueの一覧
#
h = {:one => 1, :two => 2, :three => 3}
h.keys     #=> [:one, :two, :three]
h.values   #=> [1, 2, 3]
h.to_a     #=> [[:one, 1], [:two, 2], [:three, 3]]


# hash結合
#
# merge

a = {:one => 1, :two => 2, :three => 3, :other => 999}
b = {:four => 4, :five => 5, :other => 10}

# merge
a.merge b    #=> {:one=>1, :two=>2, :three=>3, :other=>10, :four=>4, :five=>5}

# merge block利用 keyが重複した場合の評価結果を返す
a = {:one => 1, :two => 2, :three => 3, :other => 999}
b = {:four => 4, :five => 5, :other => 10}
a.merge(b) { |k,v1,v2|  "#{k},#{v1},#{v2}"  }
   #=> {:one=>1, :two=>2, :three=>3, :other=>"other,999,10", :four=>4, :five=>5}

関連記事

トラックバック(0)

トラックバックURL: http://mukaer.com/cgi-bin/mt/mt-tb.cgi/86

コメントする

PR

PR





検索

Loading

メニュー

twitter