Managing Whitespace

Problem

Your string contains too much whitespace, not enough whitespace, or the wrong kind of whitespace.

Solution

Use strip to remove whitespace from the beginning and end of a string:

	" 	Whitespace at beginning and end. 	

". 
strip

Add whitespace to one or both ends of a string with ljust, rjust, and center:

	s = "Some text."
	s. 
center(15)
	s. 
ljust(15)
	s. 
rjust(15)

Use the gsub method with a string or regular expression to make more complex changes, such as to replace one type of whitespace with another.

	#Normalize Ruby source code by replacing tabs with spaces
	rubyCode.gsub("	", " ")

	#Transform Windows-style newlines to Unix-style newlines
	"Line one

Line two

".gsub(

", "
")
	# => "Line one
Line two
"

	#Transform all runs of whitespace into a single space character
	"

This string			uses
 all	sorts
of whitespace.".gsub(/s+/," ")
	# => " This string uses all sorts of whitespace."

 

Discussion

What counts as whitespace? Any of these five characters: space, tab ( ), newline ( ), linefeed ( ), and form feed (f). The regular expression /s/ matches any one character from that set. The strip method strips any combination of those characters from the beginning or end of a string.

In rare cases you may need to handle oddball "space" characters like backspace ( or 10) and vertical tab (v or 12). These are not part of the s character group in a regular expression, so use a custom character group to catch these characters.

	" It's whitespace, Jim,vbut not as we know it.
".gsub(/[sv]+/, " ")
	# => "It's whitespace, Jim, but not as we know it."

To remove whitespace from only one end of a string, use the lstrip or rstrip method:

	s = " Whitespace madness! "
	s.lstrip # => "Whitespace madness! "
	s.rstrip # => " Whitespace madness!"

The methods for adding whitespace to a string (center, ljust, and rjust) take a single argument: the total length of the string they should return, counting the original string and any added whitespace. If center can't center a string perfectly, it'll put one extra space on the right:

	"four".center(5) # => "four "
	"four".center(6) # => " four "

Like most string-modifying methods, strip, gsub, lstrip, and rstrip have counterparts strip!, gsub!, lstrip!, and rstrip!, which modify the string in place.


Strings

Numbers

Date and Time

Arrays

Hashes

Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming

XML and HTML

Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration



Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net