# Need help massaging some strings

**URL:** <https://www.ruby-forum.com/t/need-help-massaging-some-strings/80520>\
**Category:** Ruby\
**Created:** [December 28, 2006, 6:21am UTC](https://www.ruby-forum.com/t/need-help-massaging-some-strings/80520 "2006-12-28T06:21:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 28, 2006, 6:21am UTC](https://www.ruby-forum.com/t/need-help-massaging-some-strings/80520/1 "2006-12-28T06:21:33Z")

</div>

As a Rails developer, I’ve been learning Ruby on a need-to-know basis.  
However, I now need to tap its power to batch alter some very large text  
dumps and find myself a bit lost. Here is what I need to do:

Transform data in files like “alabama.txt”:  
\* White Hall, Lowndes County  
\* Wilmer, Mobile County  
\* Wilsonville, Shelby County  
\* Wilton, Shelby County  
\* Winfield, Marion County  
\* Winterboro, Talladega County

…into this format:  
White Hall  
Wilmer  
Wilsonville  
Wilton  
Winfield  
Winterboro

I have created a method for doing this in “cleanup.rb”:

def cleanup(state)  
diskfile = File.new(state + “-cleaned.txt”, “w”)  
$stdout = diskfile

IO.foreach(state + “.txt”) do |line|  
line.delete("\*")  
line.strip!  
temp = line.split(",")  
temp.delete\_at(1).to\_s  
puts temp  
end

diskfile.close  
$stdout = STDOUT  
end

So I go into IRB and here is how my session goes:

irb(main):008:0\> require ‘cleanup’  
=\> true  
irb(main):009:0\> cleanup(alabama)  
NameError: undefined local variable or method `alabama’ for  
main:Object  
irb(main):010:0\> cleanup  
ArgumentError: wrong number of arguments (0 for 1)

So my method is loaded but isn’t working. Could anyone offer any advice  
on getting this thing running? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 28, 2006, 6:37am UTC](https://www.ruby-forum.com/t/need-help-massaging-some-strings/80520/2 "2006-12-28T06:37:24Z")

</div>

> irb(main):009:0\> cleanup(alabama)  
> NameError: undefined local variable or method `alabama’ for

Got it. What a doofus - I just had to use this instead:

irb(main):009:0\> cleanup(“alabama”)

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 28, 2006, 7:37am UTC](https://www.ruby-forum.com/t/need-help-massaging-some-strings/80520/3 "2006-12-28T07:37:15Z")

</div>

Taylor S. wrote:

> ```
> * Winterboro, Talladega County
> 
> ```
> 
> end  
> NameError: undefined local variable or method `alabama’ for  
> main:Object  
> irb(main):010:0\> cleanup  
> ArgumentError: wrong number of arguments (0 for 1)

def cleanup(state)  
File.open(state + “-cleaned.txt”, “w”) do |outfile|  
IO.foreach(state + “.txt”) do |line|  
outfile.puts line[/^[\s\*]_(._),[^,]\*$/, 1 ]  
end  
end  
end

Or:

def cleanup(state)  
File.open(state + “-cleaned.txt”, “w”) { |out|  
out.puts IO.readlines(state + “.txt”).map{|line|  
line[/^[\s\*]_(._),[^,]\*$/, 1 ] } }  
end

Or even:

def cleanup(s)  
open(s+"-cleaned.txt",“w”){|o|o\<\<IO.read(s+".txt").  
gsub(/^[_]_|,.\*$/,"")}  
end

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 28, 2006, 6:39am UTC](https://www.ruby-forum.com/t/need-help-massaging-some-strings/80520/4 "2006-12-28T06:39:07Z")

</div>

On Dec 27, 2006, at 21:21, Taylor S. wrote:

> ```
> * Winfield, Marion County
> 
> ```
> 
> I have created a method for doing this in “cleanup.rb”:  
> puts temp  
> irb(main):009:0\> cleanup(alabama)  
> NameError: undefined local variable or method `alabama’ for  
> main:Object

This is your clue. You didn’t define the alabama variable or method.

> irb(main):010:0\> cleanup  
> ArgumentError: wrong number of arguments (0 for 1)

You have to pass one argument.

> So my method is loaded but isn’t working. Could anyone offer any  
> advice  
> on getting this thing running? Thanks in advance.

cleanup ‘alabama’

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 28, 2006, 10:51am UTC](https://www.ruby-forum.com/t/need-help-massaging-some-strings/80520/5 "2006-12-28T10:51:26Z")

</div>

I think what the original author of this message was trying to say  
was “Have you tryed RegExps?”  
=P

Paulo Jorge Duarte  
Köremoved\_email\_address@domain.invalid
