# Why dosnt this work

**URL:** <https://www.ruby-forum.com/t/why-dosnt-this-work/185051>\
**Category:** Ruby\
**Created:** [March 12, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/why-dosnt-this-work/185051 "2010-03-12T14:32:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![murloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@murloc](https://www.ruby-forum.com/u/murloc)\
**Post date:** [March 12, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/why-dosnt-this-work/185051/1 "2010-03-12T14:32:45Z")

</div>

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 : /

---

<div class="post-metadata">

**Author:** ![murloc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ebca7d/32.png) [@murloc](https://www.ruby-forum.com/u/murloc)\
**Post date:** [March 12, 2010, 3:07pm UTC](https://www.ruby-forum.com/t/why-dosnt-this-work/185051/2 "2010-03-12T15:07:51Z")

</div>

> 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’)
