How to solve undefined error

My program as follow:

subjectName = {}
subjectMark = {}
n = 1
while n <= 3
puts ‘Enter the subject name’
subjectName = gets.chomp

puts ‘Enter the subject marks’
subjectMark = gets.to_i
n +=1;
end

subjectName.each {|key, value|
puts " #{subjectName} ==>#{subjectMark}" }

I am trying to run above program, I got undefined error like:

" marksheet.rb:14:in <main>': undefined methodeach’ for “t”:String
(NoMethodError) "

I can’t able to get above error.

How can I get desire output?

Thank you.

Please read how Hashes works, and then try again. Your mistake was in
case where you’re changing type of Hash to String asigning your hash var
with gets.chomp.


Iurii Plugatariov
Sent with Airmail

On March 4, 2014 at 17:02:55, Jaimin P. ([email protected])
wrote:

My program as follow:
subjectName = {}
subjectMark = {}
n = 1
while n <= 3
puts ‘Enter the subject name’
subjectName = gets.chomp

puts ‘Enter the subject marks’
subjectMark = gets.to_i
n +=1;
end

subjectName.each {|key, value|
puts " #{subjectName} ==>#{subjectMark}" }

I am trying to run above program, I got undefined error like:

" marksheet.rb:14:in <main>': undefined method each’ for “t”:String
(NoMethodError) "

I can’t able to get above error.

How can I get desire output?

Thank you.


Posted via http://www.ruby-forum.com/.

the solution could be like this(fast and sketchy way):

subject_store = {}
marks = []
subjects = []

3.times { puts ‘Enter subject name’; subjects << gets.chomp }
3.times { puys ‘Enter subject marks’; marks << gets.to_i }
subject_store = Hash[marks.zip(subjects)]
subject_store…each {|key, value|
puts " #{subjectName} ==>#{subjectMark}" }


Iurii Plugatariov
Sent with Airmail

On March 4, 2014 at 17:02:55, Jaimin P. ([email protected])
wrote:

My program as follow:
subjectName = {}
subjectMark = {}
n = 1
while n <= 3
puts ‘Enter the subject name’
subjectName = gets.chomp

puts ‘Enter the subject marks’
subjectMark = gets.to_i
n +=1;
end

subjectName.each {|key, value|
puts " #{subjectName} ==>#{subjectMark}" }

I am trying to run above program, I got undefined error like:

" marksheet.rb:14:in <main>': undefined method each’ for “t”:String
(NoMethodError) "

I can’t able to get above error.

How can I get desire output?

Thank you.


Posted via http://www.ruby-forum.com/.