Jump to content

Method Help with Ruby

flowalex
Go to solution Solved by flowalex,

Thank you for your help, I was talking with a classmate and they noticed I made a mistake in the method creation.  Here is the code for future reference

#method creationdef to_array letter  return letter.chars.to_aenddef get_permutations letter_array  return letter_array.permutation.map(&:join)end#asking for user inputprint "Please enter a string of letters: "input_str = gets.chomp#opening the dictionarydictionary = File.open("dict.txt", "r") #assigning the arrayarray = to_array(input_str)#getting the permutationsperms = get_permutations(array)#while loop that compares the permutations with the unix dictionarywhile line = dictionary.gets  perms.each do |perm|    if line.chomp.downcase == perm.downcase      print perm, "\n"      exit    end  endend#closes the dictionary filedictionary.close

So for class I am having issues with the method. Any help is appreciated 

edit added comments to code

def array?(input_str)  letter_array = to_array(input_str)enddef perm  perms = get_permutations(letter_array)  letter = perms.join.downcase end#asks to enter a string of jumble lettersprint "Please enter a string of letters: "input_str = gets.chomp#opens the unix dictionary filedictionary = File.open("dict.txt", "r") #while loop that compares the input to the dictionarywhile line = dictionary.gets  word = line.chomp  perms.each do |perm|  if word == perm    print word, "\n"    exit          end    end

 

Link to comment
Share on other sites

Link to post
Share on other sites

What are the issues. What is the method supposed to do. Comment your code. Debug it. Make an effort.

Link to comment
Share on other sites

Link to post
Share on other sites

I am getting this for the error

Please enter a string of letters: dgo

filename.rb:17:in `<main>': undefined local variable or method `perms' for main:Object (NameError)

 

What are the issues. What is the method supposed to do. Comment your code. Debug it. Make an effort.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

I am getting this for the error

Please enter a string of letters: dgo

filename.rb:17:in `<main>': undefined local variable or method `perms' for main:Object (NameError)

 

It means that on line 17, you have an undefined variable named "perms". If a variable isn't defined, it means that you need to define it, e.g. assign a value. Where is the assignment happening for that variable?

Link to comment
Share on other sites

Link to post
Share on other sites

It means that on line 17, you have an undefined variable named "perms". If a variable isn't defined, it means that you need to define it, e.g. assign a value. Where is the assignment happening for that variable?r 

Here I updated the code


 
def letter_array?(input_str)
  letter_array = to_array(input_str)
end
def perms
  perms = get_permutations(letter_array)
  letter = perms.join.downcase 
end
#asks to enter a string of jumble letters

print "Please enter a string of letters: "

input_str = gets.chomp

#opens the unix dictionary file

dictionary = File.open("dict.txt", "r")

#while loop that compares the input to the dictionary

while line = dictionary.gets

word = line.chomp

perms.each do |perm|

if word == perm

print word, "\n"

exit

end

end

Now I am getting this error

filename.rb:9:in `perms': undefined local variable or method `letter_array' for main:Object (NameError)

from filename.rb:17:in `<main>'

 

Link to comment
Share on other sites

Link to post
Share on other sites

Okay, so what did you change here and why?

Added the s to def perm, because i forgot it

 

Link to comment
Share on other sites

Link to post
Share on other sites

Added the s to def perm, because i forgot it

 

Fine, fine. so line 9, what is happening? Or what do you intend to have happen there?

Link to comment
Share on other sites

Link to post
Share on other sites

I want it to get the permutations of the input, the plan of the code is to solve a word jumble puzzle

 

Link to comment
Share on other sites

Link to post
Share on other sites

I want it to get the permutations of the input, the plan of the code is to solve a word jumble puzzle

 

And how is it getting the permutations of the input? What is the actual code itself doing (e.g. by calling a function, storing a result in a variable, ect)

Link to comment
Share on other sites

Link to post
Share on other sites

Well according to my professor this is how it would work: 

 perms = get_permutations(letter_array)

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

Well according to my professor this is how it would work: 

 perms = get_permutations(letter_array)

 

Can you explain what each element in that line is doing? What is "perms"? What does the "=" do? What does "letter_array" mean here?

Link to comment
Share on other sites

Link to post
Share on other sites

perms is the 

= is the assignment operator

letter_array is the letters that have been input by the user

 

Link to comment
Share on other sites

Link to post
Share on other sites

perms is the 

= is the assignment operator

letter_array is the letters that have been input by the user

 

If letter_array is the input, where is it assigned?

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you for your help, I was talking with a classmate and they noticed I made a mistake in the method creation.  Here is the code for future reference

#method creationdef to_array letter  return letter.chars.to_aenddef get_permutations letter_array  return letter_array.permutation.map(&:join)end#asking for user inputprint "Please enter a string of letters: "input_str = gets.chomp#opening the dictionarydictionary = File.open("dict.txt", "r") #assigning the arrayarray = to_array(input_str)#getting the permutationsperms = get_permutations(array)#while loop that compares the permutations with the unix dictionarywhile line = dictionary.gets  perms.each do |perm|    if line.chomp.downcase == perm.downcase      print perm, "\n"      exit    end  endend#closes the dictionary filedictionary.close

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×