# Private Method Error

**URL:** <https://www.ruby-forum.com/t/private-method-error/107767>\
**Category:** Ruby\
**Created:** [June 26, 2007, 1:13am UTC](https://www.ruby-forum.com/t/private-method-error/107767 "2007-06-26T01:13:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [June 26, 2007, 1:13am UTC](https://www.ruby-forum.com/t/private-method-error/107767/1 "2007-06-26T01:13:59Z")

</div>

Once again, more questions.

So this is pertaining to my implementation of the name picker app.  
When I pick a random name, I then proceed to check if it matches any  
name in the used\_names file. However, I keep getting a private method  
error when I first choose a random line.  
ERROR  
-----------------------------------------|  
namePicker-cli.rb:24:in `random': private method`chop’ called for  
nil:NilClass (NoMethodError)  
from namePicker-cli.rb:22:in `open' from namePicker-cli.rb:22:in`random’  
from namePicker-cli.rb:44

CODE  
----------------------------------------|  
def random

```
 until @valid == true
   File.open(@input_file) do |f| # Open the file
     lines = f.readlines # Read the lines
     chosen_one = lines[rand(lines.size) + 1].chop # Select a

```

random one  
end  
----------------------------------------|

I have no clue why I’m getting this, and I’m almost in tears because  
my computer doesn’t like me. Help?

-------------------------------------------------------|  
~ Ari  
crap my sig won’t fit

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [June 26, 2007, 1:44am UTC](https://www.ruby-forum.com/t/private-method-error/107767/2 "2007-06-26T01:44:02Z")

</div>

Hi –

On Tue, 26 Jun 2007, Ari B. wrote:

> ```
> from namePicker-cli.rb:22:in `open'
> chosen_one = lines[rand(lines.size) + 1].chop # Select a random one
> end
> 
> ```
> 
> ----------------------------------------|
> 
> I have no clue why I’m getting this, and I’m almost in tears because my  
> computer doesn’t like me. Help?

Let’s say lines has 10 elements. rand(11) gives you a number between  
0 and 10, inclusive. Adding 1 gives you a number between 1 and 11.  
Since the last index of lines is 9, that means that if you get 10 or  
11, you’re indexing nil (lines[10] or lines[11]).

So… you probably want lines[rand(lines.size)].

(You probably also want to do something to update @valie 🙂

David

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [June 26, 2007, 2:06am UTC](https://www.ruby-forum.com/t/private-method-error/107767/3 "2007-06-26T02:06:04Z")

</div>

On Jun 25, 2007, at 7:43 PM, removed\_email\_address@domain.invalid wrote:

> Let’s say lines has 10 elements. rand(11) gives you a number between  
> 0 and 10, inclusive. Adding 1 gives you a number between 1 and 11.  
> Since the last index of lines is 9, that means that if you get 10 or  
> 11, you’re indexing nil (lines[10] or lines[11]).
> 
> So… you probably want lines[rand(lines.size)].

Ahh! Okay! Thanks!

> (You probably also want to do something to update @valie 🙂  
> Don’t worry, I do. I didn’t want to show my code for fear of  
> violating something.

~ Ari  
English is like a pseudo-random number generator - there are a  
bajillion rules to it, but nobody cares.
