Why dosnt this work

D

David Vlad

Hey guys

I get an error on this code:

def print_options
puts "What do you want to do? Enter the number of the program you
want to run."
puts "1. Show different classes."
puts "2. Search for a student."
end


def showclasses
classlists = Dir.glob "*txt"
puts "Which class do you want to view?"
puts classlists
classanswer = gets.chomp.downcase
classlists.each do |klass|
pupils = Files.readlines klass
if klass == classanswer
puts "The following students are in #{klass.gsub(".txt",
":")}"
puts pupils
end
end
end


def studentsearch
classlists = Dir.glob "*txt"
puts "Type in the student you want to search for."
search = gets.capitalize
classlists.each do |klass|
elever = File.readlines(klass)
if elever.include? search
puts "#{search.chomp} is in #{klass.gsub(".txt", ".")}"
end
end
end


print_options

answer = gets.chomp

options = case answer
when "1" then showclasses
when "2" then studentsearch
end


it's the 'def showclasses' part that causes me problems. The loop dosnt
work for some reason and I dont get why : /
 
B

Brian Candler

it's the 'def showclasses' part that causes me problems. The loop dosnt
work for some reason and I dont get why : /

Add some 'puts' or 'p' statements to your code in strategic places, e.g.
just before, inside, and after the loop.

p klass

p pupils

p klassanswer

etc. ('p foo' is short for 'puts foo.inspect')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top