Jump to content

Method Help with Ruby

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
https://linustechtips.com/topic/370332-method-help-with-ruby/
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
https://linustechtips.com/topic/370332-method-help-with-ruby/#findComment-5014853
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
https://linustechtips.com/topic/370332-method-help-with-ruby/#findComment-5014893
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
https://linustechtips.com/topic/370332-method-help-with-ruby/#findComment-5014969
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
https://linustechtips.com/topic/370332-method-help-with-ruby/#findComment-5025506
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

×