edit SideBar
|
Ruby.Hashes History
Hide minor edits - Show changes to markup
May 30, 2008, at 06:40 PM
by 78.188.55.33
Added lines 112-117:
May 30, 2008, at 06:16 PM
by 78.188.55.33
Changed lines 105-106 from:
to:
Changed lines 109-110 from:
to:
May 30, 2008, at 06:16 PM
by 78.188.55.33
Changed lines 105-111 from:
to:
=]
- hash'in key'lerine g�re sıralama yapmak.
h = {"1" => "bir", "7" => "yedi", "2" => "iki"}
h.keys.sort {|a,b| a.to_i <=> b.to_i}.each do |key|
puts h[key]
end
November 29, 2006, at 11:00 PM
by 85.105.66.26
Added lines 1-105:
(:code:)
#!/usr/bin/ruby
#
# Hashes
#
# hash oluşturulması
h = { "a" => 1, "b" => 2 }
h2 = Hash.new #yeni hash oluşturulur
h2[:ad] = "Oz" #hash'e değer atanır
h2[:soyad] = "Ko" #hash'e değer atanır
#hash'in elemanlarına ulaşılması
h["a"]
h["b"]
#hash'e yeni eleman eklenmesi
h["c"] = 3;
#Like an array, a hash contains references to objects, not copies of them.
#Modifications to the original objects will affect all references to them:
motto = "Don't thread on me"
flag = { :motto => motto, :picture => "picture.png" }
motto.upcase!
flag[:motto]
#Hash'lerde key olarak symbol kullanılması �nerilir. bu iş 2 yolla yapılabilir.
#Using symbols instead of strings saves memory and time.
#It saves memory because there's only one symbol instance, instead of many string instances.
#If you have many hashes that contain the same keys, the memory savings adds up.
#Using symbols as hash keys is faster because the hash value of a symbol is simply its object ID.
#If you use strings in a hash, Ruby must calculate the hash value of a string each time it's used as a hash key.
h = Hash.new
h[:name] = "Oz" # veya h['name'.intern] = "Oz";
# �ntanımlı değeriyle birlikte hash oluşuturulması
h = Hash.new("default value")
h[1] #default value
h['heyyo'] #default value
h[:x] #default value
# İki hash'in birleştirilmesi
h1 = { "a"=> 1, "b"=>2 }
h2 = { "c"=> 3, "d"=>4 }
h1.merge!(h2)
# Bir hash'in değerinin başka bir hash ile değiştirilmesi
h1 = { "a"=> 1, "b"=>2 }
h2 = { "a"=> 3, "b"=>4 }
h1.replace(h2)
#Hash'ten eleman silinmesi
h = { 1 => "aaa" }
h.delete(1)
#hash'in key'lerine ulaşılması
h = { "a"=> 1, "b"=>2 }
h.keys
#hash'in value'lerine ulaşılması
h = { "a"=> 1, "b"=>2 }
h.values
#hash'in eleman sayısının bulunması
h = { "a"=> 1, "b"=>2 }
h.keys.size
#Hash'in t�m elemanlarını silmek i�in
h = { "a"=> 1, "b"=>2 }
h.clear
#Hash'in key,value �iftleri arasında gezinme
h = { "a"=> 1, "b"=>2, "d" => 4, "c" => 3}
h.each_pair { |key, value| puts "key: #{key} value: #{value}" }
#Hash'in keyleri arasında gezinme
h = { "a"=> 1, "b"=>2, "d" => 4, "c" => 3}
h.each_key { |key| puts key }
#Hash'in value'leri arasında gezinme
h = { "a"=> 1, "b"=>2, "d" => 4, "c" => 3}
h.each_value { |val| puts val }
#Hash'in diziye d�n�şt�r�lmesi
h = { "a"=> 1, "b"=>2, "d" => 4, "c" => 3}
h.to_a
#Hash'in keylere g�re sıralanması
h = { "a"=> 1, "b"=>2, "d" => 4, "c" => 3}
h.keys.sort.each do |x|
puts x
end
#İ�i�e hash'lerdeki i�teki hash'in değerlerine ulaşılması
h = Hash.new
h[:phone] = { :work => "5555555", :gsm => "999999999"}
h[:phone].each_pair do |k,v|
puts "#{k} #{v}"
end
#hash'in keyleri arasında arama yapma
h = { "a"=> 1, "b"=>2, "d" => 4, "c" => 3}
h.keys.grep /[^ab]/
|