# Change in number of leading spaces

**URL:** <https://www.ruby-forum.com/t/change-in-number-of-leading-spaces/186143>\
**Category:** Ruby\
**Created:** [April 1, 2010, 4:01am UTC](https://www.ruby-forum.com/t/change-in-number-of-leading-spaces/186143 "2010-04-01T04:01:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xtremebain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/a6a055/32.png) [@xtremebain](https://www.ruby-forum.com/u/xtremebain)\
**Post date:** [April 1, 2010, 4:01am UTC](https://www.ruby-forum.com/t/change-in-number-of-leading-spaces/186143/1 "2010-04-01T04:01:05Z")

</div>

Sorry about re-post due to typo.

I am looping through a file and using a script that detects leading  
spaces (based on a previous post to this forum)

What I want to do is to detect changes in number of leading spaces, for  
instance when one line has more, fewer, or the same number of leading  
spaces as the previous line.

The problem I am having is with setting a value from within the loop  
which is then available on the next loop.

here is an example of the program:

infile = File.new(“input.txt”, “r”)  
outfile = File.new(“output.txt”, “w”)  
infile.each\_line {  
|i|

spaces = i[/\A \*/].length

#the following variable “stored” puts undef every time through, which  
means #it’s not retaining the value on the next loop?

if defined?(stored)== true  
puts stored

elsif defined?(stored)== nil  
puts “undef”  
stored = spaces  
end

case spaces  
when 1  
output= “onespace”

when 2  
output=“2 spaces”

end

outfile.write output  
}

outfile.close()  
infile.close()

outfile = File.new(“output.txt”, “r”)  
outfile.each\_line {  
|i|  
print “\>\>”, i  
}

---

<div class="post-metadata">

**Author:** ![xtremebain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/a6a055/32.png) [@xtremebain](https://www.ruby-forum.com/u/xtremebain)\
**Post date:** [April 1, 2010, 4:13am UTC](https://www.ruby-forum.com/t/change-in-number-of-leading-spaces/186143/2 "2010-04-01T04:13:00Z")

</div>

Your variable “stored” is only defined and used within the loop, so it  
is local to the loop, and is lost when exiting the loop code block. You  
need to either define it once outside the loop, before entering (stored  
= nil), or make it an instance variable (@stored).

–Alex

---

<div class="post-metadata">

**Author:** ![xtremebain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/a6a055/32.png) [@xtremebain](https://www.ruby-forum.com/u/xtremebain)\
**Post date:** [April 1, 2010, 9:53am UTC](https://www.ruby-forum.com/t/change-in-number-of-leading-spaces/186143/3 "2010-04-01T09:53:26Z")

</div>

2010/4/1 Jesse B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> which is then available on the next loop.
> 
> here is an example of the program:
> 
> infile = File.new(“input.txt”, “r”)

You can simplify this by using

File.foreach “input.txt” do |line|  
…  
end

> outfile = File.new(“output.txt”, “w”)

Also, please make it a habit to use the block form of File.open which  
is much safer. So this becomes

File.open(“output.txt”, “w”) do |outfile|  
…  
end

For some background:  
[http://blog.rubybestpractices.com/posts/rklemme/001-Using\_blocks\_for\_Robustness.html](http://blog.rubybestpractices.com/posts/rklemme/001-Using_blocks_for_Robustness.html)

> infile.each\_line {  
> |i|
> 
> spaces = i[/\A \*/].length
> 
> #the following variable “stored” puts undef every time through, which  
> means #it’s not retaining the value on the next loop?
> 
> if defined?(stored)== true  
> puts stored

As Alex already pointed out this should be defined outside the loop.

Here’s another variant

def count\_leading\_spaces(s)  
s[/\A \*/].length # or any other method that works  
end

sp = 0

ARGF.each\_line do |line|  
i = count\_leading\_spaces line

if i != sp  
line.chomp! # avoid duplicate \n  
printf “Change from %2d to %2d spaces in this line %4d: %s\n”,  
sp, i, ARGF.lineno, line  
sp = i  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![xtremebain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/a6a055/32.png) [@xtremebain](https://www.ruby-forum.com/u/xtremebain)\
**Post date:** [April 4, 2010, 11:37pm UTC](https://www.ruby-forum.com/t/change-in-number-of-leading-spaces/186143/4 "2010-04-04T23:37:40Z")

</div>

Robert, Thank you for taking the time to write back.  
I got the initial file operations you gave me working like so:

File.open(“output.txt”, “w”) do |outfile|  
File.foreach “input.txt” do |line|  
puts line  
outfile.write line  
end  
end

which both displays the results on the command line and in the  
output.txt file

then I tried to add in the rest like so

File.open(“output.txt”, “w”) do |outfile|  
File.foreach “input.txt” do |line|

def count\_leading\_spaces(s)  
s[/\A \*/].length # or any other method that works  
end

sp = 0

ARGF.each\_line do |line|  
i = count\_leading\_spaces line

if i != sp  
line.chomp! # avoid duplicate \n  
printf “Change from %2d to %2d spaces in this line %4d: %s\n”,  
sp, i, ARGF.lineno, line  
sp = i  
end  
end

#puts line  
#outfile.write line  
end  
end

and when I save as a ruby file and run from the command line nothing  
happens, until i hit control-c and get a list of errors.

I think the problem is that I am incorrectly assembling your snippets.

Would it be possible to redo this as a complete example that works as a  
ruby file run from the command line?

thank you in advance
