Jump to content

Ruby - undefined method (NoMethodError)?

Flanelman

Hey guys, So I'm new to coding in Ruby and i'm getting the following error:

in 'main': undefined method 'wordArr' for main:Object (NoMethodError)

here's my code:
 

def main

	wordArr = Array.new
	lettersArray = Array.new
	
	def readInText(file)
		fileName = file.to_s 
		$wordArr = File.readlines(fileName) 
	end
	readInText("hangmanWords.txt") 

	puts " 
 _                                             
| |                                            
| |__   __ _ _ __   __ _ _ __ ___   __ _ _ __  
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ 
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
                    __/ |                      
                   |___/ 
				   "
	puts "Welcome to hangman! type quit at any point to exit, good luck!"

	random = rand(wordArr.length) #choose a rand num between 0-array length (number of words)
	word = wordArr(random) #gets a random word from the array of words
	puts "The word has been chosen! please enter the letter you wish to guess!"
	
	puts "Please enter a letter!"
	guess = gets.chomp 
	if(guess == "quit")
		puts "Thank you for playing!"
		sleep(2.5) 
		exit
	end

end
main

From researching online as far as I could tell people get this error when calling something before it's declared, but I create the array first, then use it within the method, then call it to get the length(which is where the error is because it's printing the welcome message before then getting the error)? Only thing I can think is there's something going on with maybe having different instances of the same array?

Any help would be greatly appreciated :)

Link to comment
Share on other sites

Link to post
Share on other sites

I haven't worked with Ruby, so .. sorry if I'm wrong

the code seems to be some mashup between Pascal and C

 

what I (don't) see from your code:

  • random - not defined, integer?!
  • word - not defined, string?!
  • guess - not defined, string?!

 

----

 

back in the day I loved me some Borland editors and compilers since it allowed you to execute code line by line

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not fluent in Ruby but it seems to me that the problem might be the fileName. Don't you have to declare fileName as string first?

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Naeaes said:

I'm not fluent in Ruby but it seems to me that the problem might be the fileName. Don't you have to declare fileName as string first?

Oh I'm not actually sure I might have to look into that.

edit: if that was needed, surley it wouldnt run at all? rather than running through part and then crashing? 

 

1 hour ago, zMeul said:

I haven't worked with Ruby, so .. sorry if I'm wrong

the code seems to be some mashup between Pascal and C

 

what I (don't) see from your code:

  • random - not defined, integer?!
  • word - not defined, string?!
  • guess - not defined, string?!

 

----

 

back in the day I loved me some Borland editors and compilers since it allowed you to execute code line by line

I'm pretty sure that Ruby is like Javascript in that regard, where you don't have to declare variables.

I've only ever coded in c#, java and c++ So in terms of syntax etc It's probably not like how most people would write ruby :)

Link to comment
Share on other sites

Link to post
Share on other sites

On 11/8/2016 at 2:43 AM, sgzUk74r3T3BCGmRJ said:

snip


Okay I'll go look into them now, Appreciate the help!

edit:
One more thing, when I try and run my new code I get the following error:
          undefined local variable or method 'hangmanWords' for main:Object (nameError)

heres the code within the file:
 

def Read_Into_Array(file)
	file_Name = file.to_s
	$word_Arr = File.readlines(file_Name)
end

Read_Into_Array(hangmanWords.txt)

And I've checked that the file name is right, and they're in the same dir so I'm a little confused, does it think it's a variable? and if so, how do I solve this?

Thanks again :)

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, sgzUk74r3T3BCGmRJ said:

You have syntax issues on every line in the block of code. I encourage to go back and start at the beginning to learn how the language is written, how to read parser warnings, and how to write it correctly. You probably don't want that method definition at all: it's got at least one 'problem' per line and it doesn't need to exist at all.


Read_Into_Array(hangmanWords.txt)

Ruby doesn't support bare strings—at least not out-of-the-box—so `hangmanWords` is a name error, and `txt` is an undefined method called on that undefined local variable or method. The parser is telling you that in the error message.

 

Quote your strings.

 

As I said in the last post, you probably wanted to replace all of that complexity with this:


wordArr = File.readlines("hangmanWords.txt")

---

If you've been working professionally with C# or Java for a few years then you can probably get by with a quirky book like Why's poignant guide but there are also options like doing it the hard way or you can go with classics of the language like the pick axe book. In any case, I think your code looks like the product of a person too concerned with "making something—anything—work" and not enough with "learning a language". If you just want to throw some text into a file until something sticks then good luck. If you want to learn the language you should take a step back and go back over basic syntax before you continue.

Nah not professionally, I'm still pretty new with them all recently started doing an I.T. course so been mainly picking it up there :)

And yeah that might be a better Idea to actually read into ruby a little more, thank you for your help!

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

×