# How to remove empty space in a string and others

**URL:** <https://www.ruby-forum.com/t/how-to-remove-empty-space-in-a-string-and-others/71503>\
**Category:** Ruby\
**Created:** [October 9, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/how-to-remove-empty-space-in-a-string-and-others/71503 "2006-10-09T15:13:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 9, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/how-to-remove-empty-space-in-a-string-and-others/71503/1 "2006-10-09T15:13:32Z")

</div>

Hi all,

1. I read lines from a file and put them into one string. There are many  
empty sapces within the string. Which method is used to remove the  
spaces? I try gsub!("\s",’’) but it doesn’t work. Any help will be  
appreciated.

2. I read each line from a file and split each of them into several  
fields/segments. Then I need to populate an array with one field only  
from each line. How do I do that? I know do these in Perl but have no  
idea using Ruby.

3. How do I search help or some methods/predifined variables from ruby  
command prompt? Is it possible to do that? Right now the only thing I  
can do is to open the folder containing the manual and read them in IE  
browser. Is this the Ruby way?

Thanks in advance,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 9, 2006, 3:53pm UTC](https://www.ruby-forum.com/t/how-to-remove-empty-space-in-a-string-and-others/71503/2 "2006-10-09T15:53:40Z")

</div>

On Oct 9, 2006, at 9:13 AM, Li Chen wrote:

> Hi all,
> 
> 1. I read lines from a file and put them into one string. There are  
> many  
> empty sapces within the string. Which method is used to remove the  
> spaces? I try gsub!(“\s”,‘’) but it doesn’t work. Any help will be  
> appreciated.

Try gsub(/\s/, ‘’)

> 1. I read each line from a file and split each of them into several  
> fields/segments. Then I need to populate an array with one field only  
> from each line. How do I do that? I know do these in Perl but have no  
> idea using Ruby.

Would need more information about your code to answer this.

> 1. How do I search help or some methods/predifined variables from ruby  
> command prompt? Is it possible to do that? Right now the only thing I  
> can do is to open the folder containing the manual and read them  
> in IE  
> browser. Is this the Ruby way?

There’s nothing wrong with your way, but there are many other ways to  
get help while coding. Here are two you might consider:

1. visit and download from

The Ruby Cheatsheet is especially good for looking up predefined  
variables.

1. Open another terminal window and use ri to get help on classes and  
methods. For example:

Regards, Morton

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 9, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/how-to-remove-empty-space-in-a-string-and-others/71503/3 "2006-10-09T18:40:37Z")

</div>

> > 1. I read each line from a file and split each of them into several  
> > fields/segments. Then I need to populate an array with one field only  
> > from each line. How do I do that? I know do these in Perl but have no  
> > idea using Ruby.
> 
> Would need more information about your code to answer this.

I have a file called array.txt with many lines and each line is a record  
containing different fields separated by \t. What I like to do is read  
the file line by line, split each line into elements, and push the  
element [1] from each line into a new array.

# array.txt

A B C  
1 2 3  
X Y Z  
…

expected output in new array  
[B,2,Y,…]

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 9, 2006, 7:54pm UTC](https://www.ruby-forum.com/t/how-to-remove-empty-space-in-a-string-and-others/71503/4 "2006-10-09T19:54:09Z")

</div>

> > 
> 
> output = []  
> File.open( “array.txt” ) { |file| file.readlines { |curr\_line| output  
> \<\< curr\_line.split( /\t/ )[1] } }
> 
> # just to show the result
> 
> require ‘pp’  
> pp output

I need more information about file reading. Which chapter in the  
Programming Ruby talks about this stuff?

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [October 9, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/how-to-remove-empty-space-in-a-string-and-others/71503/5 "2006-10-09T18:48:11Z")

</div>

Li Chen wrote:

> the file line by line, split each line into elements, and push the  
> [B,2,Y,…]
> 
> Thanks,
> 
> Li

output = []  
File.open( “array.txt” ) { |file| file.readlines { |curr\_line| output  
\<\< curr\_line.split( /\t/ )[1] } }

# just to show the result

require ‘pp’  
pp output
