Ruby: Confused about how to make sense of this code -


i'm little confused 1 part of code. in line 7 have commented below.

01:states_file = file.open("states_abbrev.txt") 02:states = {} 03:while ! states_file.eof? 04:     first = states_file.gets.chomp 05:     #"alabama,al" 06:     data = first.split(",") 07:     states[ data[0] ] = data[1] #this line here. 08:end 09:puts states.inspect 10: 11:states_file.close 

line 5 , example of each line in states_abbrev.txt file. state, comma, abbreviation, , carriage return. 50 states in file.

as can see on line 7 data[0] key seems overwritten data[1]. why when run code data[0] still key, , data[1] becomes value?

after line 6

data[0] alabama, data[1] al

after line 7

states { 'alabama' => 'al' }

its not overwriting data[0].. data[0] key , data[1] value.

one thing can try ruby's irb


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -