himitsu girlsの日記

 | 

2006-11-04

[]第13章 練習問題(2)

順番を保存するハッシュ、OrderdHashクラスを定義

class OrderdHash
  def initialize
    @keys = []
    @h = {}
  end
  
  def keys
    return @keys.clone
  end
  
  def values
    ret = []
    @keys.each {|i|
      ret << @h[i]
    }
    return ret
  end
  
  def []=(key, value)
    @keys << key unless @keys.include?(key)
    @h[key] = value
    return value
  end
end
oh = OrderdHash.new
oh["one"] = 1
oh["two"] = 2
oh["three"] = 3
oh["two"] = 4
p oh.keys
p oh.values
結果
["one", "two", "three"]
[1, 4, 3]

keysはこれでいいのか?

EternityEternity2011/09/08 14:58Yo, that's what's up turhtfully.

mvemgazmmvemgazm2011/09/09 20:5260QbFU , [url=http://onsvptwgjsvw.com/]onsvptwgjsvw[/url], [link=http://ghpfcdxyhhsl.com/]ghpfcdxyhhsl[/link], http://odvaxjqnvoyb.com/

kmhetftuhkmhetftuh2011/09/10 18:536CxlRR <a href="http://ansvulibehxe.com/">ansvulibehxe</a>

tcwgrqlqalstcwgrqlqals2011/09/12 18:53PfHnuP , [url=http://atjdkkowgoeg.com/]atjdkkowgoeg[/url], [link=http://npfthiumycgi.com/]npfthiumycgi[/link], http://xpkmumzqaqvd.com/

トラックバック - http://rubyist.g.hatena.ne.jp/himitsu_girls/20061104
 |