How to get hash values ββby position in ruby?
Well, a position is something that is not well defined in the hash, because by definition it is an unordered set. However, if you insist on being able to do this, you can convert the hash to an array, and then continue your path:
irb(main):001:0> h = {:a => 1, :b => 2} => {:b=>2, :a=>1} irb(main):002:0> h.to_a => [[:b, 2], [:a, 1]] You can extend the Hash to get what you need (ruby 1.9.x):
class Hash def last # first is already defined idx = self.keys.last [idx , self[idx]] end def array_index_of(n) # returns nth data idx = self.keys[n] self[idx] end end h = {"a" => 100, "b" => 200} h.first # ["a", 100] h.last # ["b", 200] h.array_index_of(0) # => 100 h.array_index_of(1) # => 200