ruby on rails
ruby hashes
흰두부1
2020. 11. 8. 01:09
# ruby에서 이름은 hash지만 보통 다른 언어에서의 dictionary
sample_hash = {'a' => 1, 'b' => 2, 'c' => 3}
my_details = {'name' => 'mashrur', 'favcolor' => 'red'}
p my_details['favcolor']
another_hash={a:1, b:2, c:3}
# 이렇게 초기화하면 key가 string이 아닌 symbol로 초기화 됨
# 예제
p another_hash[:a]
p sample_hash.keys
p sample_hash.values
sample_hash.each do |key, value|
puts "THe class for key is #{key.class} and the value is #{value.class}"
end
my_details = {:name => 'mashrur', :favcolor => 'red'}
my_details.each do |key, value|
puts "THe class for key is #{key.class} and the value is #{value.class}"
end
another_hash[:d] = 4
another_hash[:e] = "Mushroom"
p another_hash
another_hash.each { |some_key, some_value| puts "The key is #{some_key} and The Value is #{some_value}" }
p another_hash.select { |k, v| v.is_a?(String) }
p another_hash.each { |k, v| another_hash.delete(k) if v.is_a?(String) }