How to truncate the spaces in the front of a line

Hi, guys
I am new to Ruby. Do you have any idea to truncate or remove the space
in the front of a line?
Thanks very much.

Milo

···

--
Posted via http://www.ruby-forum.com/.

irb(main):001:0> " spaces in front".gsub /^ +/,""
=> "spaces in front"
irb(main):002:0> RUBY_VERSION
=> "1.8.6"
irb(main):003:0>

Translation:
String#gsub applies the regexp given as first argument to a given String, and replaces the matches with the second argument.

The regexp "^ +" (the // notation is one of the many ways to tell Ruby we are using a regexp) means "At the beginning of the line ("^"), find any spaces(" "), and then stop ("+")". More or less, anyway (I'm not a Regexp guru). :wink:

···

On 27.12.2009 04:22, Milo Luo wrote:

Hi, guys
I am new to Ruby. Do you have any idea to truncate or remove the space
in the front of a line?

--
Phillip Gawlowski

Actually, toss that explanation where it belongs: The waste receptacle.

The regex means "match one or more spaces (" +"), at the beginning of the line("^")".

···

On 27.12.2009 04:40, Phillip Gawlowski wrote:

The regexp "^ +" (the // notation is one of the many ways to tell Ruby
we are using a regexp) means "At the beginning of the line ("^"), find
any spaces(" "), and then stop ("+")". More or less, anyway (I'm not a
Regexp guru). :wink:

--
Phillip Gawlowski

Phillip Gawlowski wrote:

Hi, guys
I am new to Ruby. Do you have any idea to truncate or remove the space
in the front of a line?

irb(main):001:0> " spaces in front".gsub /^ +/,""
=> "spaces in front"
irb(main):002:0> RUBY_VERSION
=> "1.8.6"
irb(main):003:0>

Translation:
String#gsub applies the regexp given as first argument to a given String, and replaces the matches with the second argument.

The regexp "^ +" (the // notation is one of the many ways to tell Ruby we are using a regexp) means "At the beginning of the line ("^"), find any spaces(" "), and then stop ("+")". More or less, anyway (I'm not a Regexp guru). :wink:

--
Phillip Gawlowski

a more convenient way is to use one of the strip methods

irb(main):004:0> " spaces in front".lstrip
=> "spaces in front"

irb(main):005:0> "spaces in back ".rstrip
=> "spaces in back"

irb(main):006:0> " spaces on both side ".strip
=> "spaces on both side"

···

On 27.12.2009 04:22, Milo Luo wrote:

--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely

Thanks for great explainations.
And one more question, how can you get this method so quickly? Is that
your experience or is tip there on it?

Thanks.
Milo

···

--
Posted via http://www.ruby-forum.com/.

Both. :wink:

For one, experience allows me to remember where, roughly, something is in the documentation, or if something that I want to do exists already. Then it is down to reading the documentation. :wink:

Ruby is, for the most part, logically organized (once you now the lingo).

In your example, you have a String (appending .class to anything should reveal the object's class, .class reveals that this is an Array), so you look at what methods the String class offers.

With a little experimentation in irb, you can see if what you found is useful. :slight_smile:

A few pointers:

You can access Ruby's documentation online at ruby-doc.org.

Another help is the excellent Pickaxe book (Programming Ruby, 2nd Edition is the most relevant for 1.8.x; 3rd Edition for Ruby 1.9.x), which works as a Ruby tutorial (for experienced developers, anyway), and an excellent reference to the language.

···

On 27.12.2009 05:20, Milo Luo wrote:

Thanks for great explainations.
And one more question, how can you get this method so quickly? Is that
your experience or is tip there on it?

--
Phillip Gawlowski

Phillip Gawlowski wrote:

···

On 27.12.2009 05:20, Milo Luo wrote:

Thanks for great explainations.
And one more question, how can you get this method so quickly? Is that
your experience or is tip there on it?

Both. :wink:

For one, experience allows me to remember where, roughly, something is
in the documentation, or if something that I want to do exists already.
Then it is down to reading the documentation. :wink:

Ruby is, for the most part, logically organized (once you now the
lingo).

In your example, you have a String (appending .class to anything should
reveal the object's class, .class reveals that this is an Array), so
you look at what methods the String class offers.

With a little experimentation in irb, you can see if what you found is
useful. :slight_smile:

A few pointers:

You can access Ruby's documentation online at ruby-doc.org.

Another help is the excellent Pickaxe book (Programming Ruby, 2nd
Edition is the most relevant for 1.8.x; 3rd Edition for Ruby 1.9.x),
which works as a Ruby tutorial (for experienced developers, anyway), and
an excellent reference to the language.

Cool. I should really have a try. :slight_smile:
Thank you, Phillip. You really help me a lot.

Milo
--
Posted via http://www.ruby-forum.com/\.

You are very welcome. :slight_smile:

Asking, then answering questions are a great way to learn, so I'm glad to be of assistance.

···

On 27.12.2009 06:06, Milo Luo wrote:

Cool. I should really have a try. :slight_smile:
Thank you, Phillip. You really help me a lot.

--
Phillip Gawlowski

Phillip Gawlowski wrote:

Cool. I should really have a try. :slight_smile:
Thank you, Phillip. You really help me a lot.

You are very welcome. :slight_smile:

Asking, then answering questions are a great way to learn, so I'm glad
to be of assistance.

Hi, Phillip
Here comes the new problem.
I want to use this gsub to substitue the spaces in the front of some
lines in a XML file. I have tried your method with string variable, but
it seems not work for this XML file. I check this file very carefully, I
found the spaces before some lines are 3 tabs.

<code>
require 'fileutils'

#Some basic variables
LPT_FILE_NAME="abc.xml"

File.file? LPT_FILE_NAME
#Open XML file

f = File.open(LPT_FILE_NAME)
begin
        while (line = f.readline)
                line.chomp
                if line =~ /<Properties>/
                        while ( line !~ /<\/Properties>/ && line =
f.readline)
                                #puts line.gsub /^\/t+/,"" if line =~
/<Name>/
                                if line =~ /<Name>/
                                        name = line..gsub /^\/s+/,""
                                        puts name

                                end
                        end
                end
        end
rescue EOFError
f.close
end

</code>

The abc.xml file should simplely look like this:
<code>
    <Properties>
      <Name>TestData</Name>
      <LoopCount>1</LoopCount>
      <Priority>100</Priority>
    </Properties>
</code>

Could you give me a hand?
Thank you.

Milo

···

On 27.12.2009 06:06, Milo Luo wrote:

--
Posted via http://www.ruby-forum.com/\.

Well, tabs are special characters "\t", so you'd have to write a regex to match spaces *and* \t characters, eventually mixed.

That's the point where I'd look into an XML parser, to read in the XML, and write it out again since you'd deal with loads of special characters, anyway, and regexen give me headaches. :wink:

REXML is included with Ruby 1.8.x, and its documentation available here: http://www.germane-software.com/software/rexml_doc

However, its documentation is a bit unwieldy and lacking (certainly not for the faint of heart).

Or Nokogiri (which will have to be installed extra, preferably via RubyGems): nokogiri.org
Googling for "nokogiri prettify XML", I get this link:

That should work for prettifying XML. :wink:

However, I wouldn't actually bother to prettify it. After all, XML is intended to be used by machines, and not so much humans, and XML parsers have to be able to deal with the whitespace (i.e. spaces and tabs).

···

On 27.12.2009 07:32, Milo Luo wrote:

Hi, Phillip
Here comes the new problem.
I want to use this gsub to substitue the spaces in the front of some
lines in a XML file. I have tried your method with string variable, but
it seems not work for this XML file. I check this file very carefully, I
found the spaces before some lines are 3 tabs.

--
Phillip Gawlowski

Phillip Gawlowski wrote:

···

On 27.12.2009 07:32, Milo Luo wrote:

Hi, Phillip
Here comes the new problem.
I want to use this gsub to substitue the spaces in the front of some
lines in a XML file. I have tried your method with string variable, but
it seems not work for this XML file. I check this file very carefully, I
found the spaces before some lines are 3 tabs.

Well, tabs are special characters "\t", so you'd have to write a regex
to match spaces *and* \t characters, eventually mixed.

That's the point where I'd look into an XML parser, to read in the XML,
and write it out again since you'd deal with loads of special
characters, anyway, and regexen give me headaches. :wink:

REXML is included with Ruby 1.8.x, and its documentation available here:
http://www.germane-software.com/software/rexml_doc

However, its documentation is a bit unwieldy and lacking (certainly not
for the faint of heart).

Or Nokogiri (which will have to be installed extra, preferably via
RubyGems): nokogiri.org
Googling for "nokogiri prettify XML", I get this link:
Pretty printing xhtml with nokogiri and xslt | Old Emmanuel Oga's Weblog (new one is at www.emmanueloga.com)

That should work for prettifying XML. :wink:

However, I wouldn't actually bother to prettify it. After all, XML is
intended to be used by machines, and not so much humans, and XML parsers
have to be able to deal with the whitespace (i.e. spaces and tabs).

so strange...
/s should match all the spaces, including tabs,but it doesn't work.
I will try to use XML Parser.

Milo
--
Posted via http://www.ruby-forum.com/\.

Hello,

so strange...
/s should match all the spaces, including tabs,but it doesn't work.
I will try to use XML Parser.

Well, it would be more useful if you used \s instead (wih a backslash
and not a slash). Try with

name = line.gsub(/^\s+/,"")

By the way, all useful shortcuts in regexp start with a backslash and
not a slash (usually reserved to delimit the regexp)

Cheers,

···

--
JJ Fleck
PCSI1 Lycée Kléber

Fleck Jean-Julien wrote:

Hello,

so strange...
/s should match all the spaces, including tabs,but it doesn't work.
I will try to use XML Parser.

Well, it would be more useful if you used \s instead (wih a backslash
and not a slash). Try with

name = line.gsub(/^\s+/,"")

By the way, all useful shortcuts in regexp start with a backslash and
not a slash (usually reserved to delimit the regexp)

Cheers,

Oh, I used the wrong syntax and it works now.
Thank you, Fleck. :slight_smile:

Milo

···

--
Posted via http://www.ruby-forum.com/\.