Ruby Method of the Day - String.strip!

Posted by Kelly McCauley on Sep 28, 2007

Signature

string.strip!   #=> string or nil

String.strip! is the in-place edit version of String.strip. string.strip! returns string with leading and trailing whitespace removed from string or it returns nil if no leading or trailing whitespace was found.

Examples

1
2
3
4
5
str = "\r\n\t  foo bar  \r\n\t"   #=> "\r\n\t  foo bar  \r\n\t"
str.strip!                        #=> "foo bar"
str == "\r\n\t  foo bar  \r\n\t"  #=> false
str == "foo bar"                  #=> true
str.strip!                        #=> nil

Documentation Reference

www.ruby-doc.org : String.strip!

An unproductive day...

Posted by Kelly McCauley on Sep 27, 2007

You know its going to be an unproductive day when you walk into work and the first thing you hear is that there was a power outage and then find that your workstation and the UPS to which it was connected completely dead.

It was a wet and slow drive into work. Nothing surprising. I walk up to my desk and my cube neighbor said that there was a power outage that knocked out a large chunk (500+ nodes) of our cluster and all of the infrastructure that wasn’t on UPS. They were able to bring the nodes back up without too much trouble. I assumed that my machine would be fine since it was on UPS and the power outage was only for a couple of seconds. My cube was too quiet. My workstation was not powered on. Strange. I check the UPS and it was completely dead. I try to reset the breaker on it but nothing happened. OK, so the UPS didn’t have enough power to keep my machine running for a couple of seconds (or so I thought). I move the workstation power cable to a wall outlet and try to turn it on. Nothing. Joy. I then open up the case, grab a spare power supply, and plug it up. Turned it on and the cooling fans started spinning but a wonderful burning plastic/metal smell immediately wafts up in my face. Power it off. Dang. Not what I want to be doing today….

I’ve gotten used to my computers staying in the background and helping me get my work done. It is jarring when your tools break. It has been over 3 years since I have had to shop for a new Linux workstation. At one early point in my life I would have been excited about this. Now it seems like a chore.

Working with my computer, as a tool, was second nature. I was concentrating on getting work done and not really noticing the hardware. That’s a big change from 10-15 years ago. Its a change I like. I prefer working with a computer, rather than working on a computer, hence why I like being a programmer and not a sysadmin.

—Kelly

Ruby Method of the Day - String.strip

Posted by Kelly McCauley on Sep 27, 2007

Signature

string.strip    #=> new_string

The String.strip method is in the same family of methods as String.lstrip and String.rstrip.

string.strip returns a new string with the leading and trailing whitespace removed.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"foo bar  \r\n\t".strip           #=> "foo bar"
"\r\n\t  foo bar".strip           #=> "foo bar"
"\r\n\t  foo bar  \r\n\t".strip   #=> "foo bar"

text = <<-"END"
      Lorem ipsum dolor sit amet, consectetuer      \t
      adipiscing elit. Ut ac nulla.  Duis sed est.  \t
      Praesent at dui et velit tempor cursus.       \t
      Morbi purus tortor, tempus tincidunt,         \t
      placerat sit amet, dignissim quis, nulla.     \t
      Duis ipsum. Proin fringilla.
END

# Prints out each line of the above paragraph with
# both leading and trailing whitespace removed and
# the string '[eol]' appended to the end of the
# line.
text.each_line{|l| puts l.strip << '[eol]'}

Documentation Reference

www.ruby-doc.org : String.strip

Ruby Method of the Day - String.rstrip!

Posted by Kelly McCauley on Sep 26, 2007

Signature

string.rstrip!    #=> string or nil

String.rstrip! is the in-place edit version of String.rstrip. string.rstrip! returns string with trailing whitespace removed from string or it returns nil if no trailing whitespace was found.

Examples

1
2
3
4
5
6
str = "foo bar \r\n\t"    #=> "foo bar \r\n\t"
str.rstrip!               #=> "foo bar"
str == "foo bar \r\n\t"   #=> false
str == "foo bar"          #=> true
str.rstrip!               #=> nil

Documentation Reference

www.ruby-doc.org : String.rstrip!

Ruby Method of the Day - String.rstrip

Posted by Kelly McCauley on Sep 25, 2007

Signature

string.rstrip   #=> new_string

String.rstrip is the counter whitespace removal method to String.lstrip. string.rstrip returns a new string with trailing whitespace removed.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"foo bar  \r\n\t".rstrip    #=> "foo bar"

text = <<-"END"
      Lorem ipsum dolor sit amet, consectetuer      \t
      adipiscing elit. Ut ac nulla.  Duis sed est.  \t
      Praesent at dui et velit tempor cursus.       \t
      Morbi purus tortor, tempus tincidunt,         \t
      placerat sit amet, dignissim quis, nulla.     \t
      Duis ipsum. Proin fringilla.
END

# Prints out each line of the obove paragraph with
# trailing whitespace removed and the string '[eol]'
# appended to the end of the line.
text.each_line{|l| puts l.rstrip << '[eol]'}

Documentation Reference

www.ruby-doc.org : String.rstrip

Ruby Method of the Day - String.lstrip!

Posted by Kelly McCauley on Sep 24, 2007

Signature

string.lstrip!    #=> string or nil

String.lstrip! is the in-place edit version of String.lstrip. string.lstrip! returns string with leading whitespace removed from string or it returns nil if no leading whitespace was found.

Examples

1
2
3
4
5
str = "\r\n\t foo bar"    #=> "\r\n\t foo bar"
str.lstrip!               #=> "foo bar"
str == "\r\n\t foo bar"   #=> false
str == "foo bar"          #=> true
str.lstrip!               #=> nil

Documentation Reference

www.ruby-doc.org : String.lstrip!

Ruby Method of the Day - String.lstrip

Posted by Kelly McCauley on Sep 21, 2007

Signature

string.lstrip   #=> new_string

string.lstrip returns a new string with leading whitespace removed.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"\r\n\t foo bar".lstrip   #=> "foo bar"

text = <<-'END'
      Lorem ipsum dolor sit amet, consectetuer
      adipiscing elit. Ut ac nulla.  Duis sed est.
      Praesent at dui et velit tempor cursus.
      Morbi purus tortor, tempus tincidunt,
      placerat sit amet, dignissim quis, nulla.
      Duis ipsum. Proin fringilla.
END

# Prints out each line of the paragraph above with
# leading whitespace removed.
text.each_line {|l| print l.lstrip }

Documentation Reference

www.ruby-doc.org : String.lstrip

Ruby Method of the Day - String.center

Posted by Kelly McCauley on Sep 20, 2007

Signature

string.center(length)                 #=> new_string
string.center(length, padding_string) #=> new_string

The String.center method is in the same family of methods as String.ljust and String.rjust.

string.ljust(length) centers string to the given length. If length is greater than the length of string then a new string is returned that is both left and right padded to length. If length is less than or equal to the length of string, then string.center(length) returns a new string that is equal to string and no padding or truncating occurs.

Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'foo'.center(10)      #=> "   foo    "
'foo'.center(10,'x')  #=> "xxxfooxxxx"
'foo'.center(2)       #=> "foo"
'foo'.center(2,'x')   #=> "foo"

l = 'left'            #=> "left"
r = 'right'           #=> "right"
c = 'center'          #=> "center"
l.ljust(8) + c.center(8) + r.rjust(8)   #=> "left     center    right"

text = <<-'END'
Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut ac nulla.  Duis sed est.
Praesent at dui et velit tempor cursus.
Morbi purus tortor, tempus tincidunt,
placerat sit amet, dignissim quis, nulla.
Duis ipsum. Proin fringilla.
END

# Prints out each line of the paragraph above, centered.
text.each_line {|l| puts l.chomp.center(50) }

Documentation Reference

www.ruby-doc.org : String.center

Ruby Method of the Day - String.rjust

Posted by Kelly McCauley on Sep 19, 2007

Signature

string.rjust(length)                  #=> new_string
string.rjust(length, padding_string)  #=> new_string

String.rjust is the counter justification method to String.ljust. string.rjust(length) right justifies string to the given length. If length is greater than the length of string then a new string is returned that is left padded to length with spaces, the default, or with padding_string. If length is less than or equal to the length of string, then string.rjust(length) returns a new string that is equal to string and no padding or truncating occurs.

Examples

1
2
3
4
'foo'.rjust(10)       #=> "       foo"
'foo'.rjust(10,'x')   #=> "xxxxxxxfoo"
'foo'.rjust(2)        #=> "foo"
'foo'.rjust(2,'x')    #=> "foo"

Documentation Reference

www.ruby-doc.org : String.rjust

Ruby Method of the Day - String.ljust

Posted by Kelly McCauley on Sep 18, 2007

Signature

string.ljust(length)                  #=> new_string
string.ljust(length, padding_string)  #=> new_string

string.ljust(length) left justifies string to the given length. If length is greater than the length of string then a new string is returned that is right padded to length with spaces, the default, or with padding_string. If length is less than or equal to the length of string, then string.ljust(length) returns a new string that is equal to string and no padding or truncating occurs.

Examples

1
2
3
4
'foo'.ljust(10)     #=> "foo       "
'foo'.ljust(10,'x') #=> "fooxxxxxxx"
'foo'.ljust(2)      #=> "foo"
'foo'.ljust(2,'x')  #=> "foo"

Documentation Reference

www.ruby-doc.org : String.ljust

Older posts: 1 2 3