data structures - Hash map in ruby is storing key-value pair automatically -



data structures - Hash map in ruby is storing key-value pair automatically -

this way create hash map in ruby

colors = { "red" => 2, "blue" => 3 }

but in below code, did key , corresponding value specified, done in "colors" hash.

puts "enter string" text = gets.chomp words=text.split frequencies = hash.new(0) words.each {|word| frequencies [word] +=1} frequencies = frequencies.sort_by |wordy, wcounty| wordy end puts frequencies

is done in each loop or in line next "frequencies" new value assigned loop, if yes , how?

i new ruby have little knowledge of java, hard me understand.

if take @ line:

words.each {|word| frequencies [word] +=1}

you can think of this code (just example, not rubyish way write it):

i=1 words.each |word| frequencies [word] = = +1 end

the key word , corresponding value assigning i. in case wondering, do/end key values same {}. utilize when code within iterator span multiple lines.

hope helps

ruby data-structures hash

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -