# Strange, non-repetitious error

**URL:** <https://www.ruby-forum.com/t/strange-non-repetitious-error/134053>\
**Category:** Ruby\
**Created:** [April 7, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053 "2008-04-07T12:36:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![flisak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@flisak](https://www.ruby-forum.com/u/flisak)\
**Post date:** [April 7, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053/1 "2008-04-07T12:36:02Z")

</div>

Hello

I have that exception sometimes: “can’t convert false into Integer”  
This error is non-repetitious.  
This exception occur once in a while when I run progam on the same data.

sample code:

for z in 1…lines.length  
line = lines[z-1] # this is the line where error occur  
end

Is it a ruby implementation problem?

Greetings

---

<div class="post-metadata">

**Author:** ![flisak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@flisak](https://www.ruby-forum.com/u/flisak)\
**Post date:** [April 7, 2008, 1:25pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053/2 "2008-04-07T13:25:40Z")

</div>

2008/4/7, Pan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end
> 
> Is it a ruby implementation problem?

Most likely not. We can’t say without seeing more code. What type of  
object is “lines”?

Why don’t you just do

lines.each do |line|  
end

?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![flisak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@flisak](https://www.ruby-forum.com/u/flisak)\
**Post date:** [April 7, 2008, 1:33pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053/3 "2008-04-07T13:33:26Z")

</div>

I don’t use “lines.each do |line|” because I need number of line.

This is the code:

File.open(procsw[j-1],‘r+’) do |file|  
lines = file.readlines  
for z in 1…lines.length  
line = lines[z-1] #can’t convert false into integer  
if(line!=nil and line!="") then  
line.downcase.gsub(“external name”) do  
if(line.index(/’._’/)!=nil) then  
line.gsub(/’._’/) {|match|  
k = match.rindex("/")  
line = “’” + bladePath + match.slice(k…match.length)

- “\n”  
}  
else  
lines[z].gsub(/’.\*’/) {|match|  
k = match.rindex("/")  
lines[z] = “’” + bladePath +  
match.slice(k…match.length) + “\n”  
}  
end  
end  
end  
end  
file.pos = 0  
file.print lines  
file.truncate(file.pos)  
file.close  
end

---

<div class="post-metadata">

**Author:** ![flisak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@flisak](https://www.ruby-forum.com/u/flisak)\
**Post date:** [April 7, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053/4 "2008-04-07T13:43:53Z")

</div>

On Mon, Apr 7, 2008 at 7:33 AM, Pan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t use “lines.each do |line|” because I need number of line.

lines.each\_with\_index do | line, z|  
…

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![flisak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@flisak](https://www.ruby-forum.com/u/flisak)\
**Post date:** [April 7, 2008, 1:43pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053/5 "2008-04-07T13:43:17Z")

</div>

On Monday 07 April 2008, Pan K. wrote:

> I don’t use “lines.each do |line|” because I need number of line.

Do you know about Array#each\_index and Array#each\_with\_index?:

[‘a’, ‘b’, ‘c’].each\_index{|i| puts i}

Output:  
0  
1  
2

[‘a’, ‘b’, ‘c’].each\_with\_index{|obj, i| puts “i: #{i}, obj: #{obj}”}

Output:  
i: 0, obj: a  
i: 1, obj: b  
i: 2, obj: c

Stefano

---

<div class="post-metadata">

**Author:** ![flisak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@flisak](https://www.ruby-forum.com/u/flisak)\
**Post date:** [April 7, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053/6 "2008-04-07T13:54:12Z")

</div>

I will try with  
lines.each\_with\_index do | line, z|

Thank you all for replies.  
Greetings

---

<div class="post-metadata">

**Author:** ![flisak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@flisak](https://www.ruby-forum.com/u/flisak)\
**Post date:** [April 7, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053/7 "2008-04-07T14:25:54Z")

</div>

2008/4/7, Pan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I will try with
> 
> lines.each\_with\_index do | line, z|

You should change a lot more. For example, you have nested  
invocations of gsub on the same line (albeit sometimes with a  
#downcase so it’s probably not the same instance).

You also do not need the “file.close” because your block ensures the  
file will be closed properly.

Also, I do not see how z can ever be a non Fixnum in your code. Are  
you sure this is the code that produces the error? Do you happen to  
have multiple threads all of them using a z which is defined outside  
the code you show?

In your case I’d rather use #map instead of normal iteration since you  
seem to be replacing line contents with something else.

Or wait, there seems to be a off by one error lurking: you use  
1…lines.length for iteration, read lines[z-1] but later lines[z].  
lines[z] does not exist for z==lines.length. You either need to  
change logic or use the three dots range for iteration.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![flisak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/73ab20/32.png) [@flisak](https://www.ruby-forum.com/u/flisak)\
**Post date:** [April 7, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/strange-non-repetitious-error/134053/8 "2008-04-07T14:33:33Z")

</div>

On Mon, Apr 7, 2008 at 2:25 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 2008/4/7, Pan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):  
> You should change a lot more. For example, you have nested  
> …

I agree with Robert. Maybe if you describe what you are trying to  
achieve, somebody would help with a cleaner solution.

J.
