RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Ruby—A Diamond of a Programming Language, Part 2 : Page 2

Get ready to dive deeper into the power and elegance of the language lauded as a possible contender to replace current programming languages.

Strings are arbitrary sequence of bytes in Ruby. Usually they are a sequence of characters. In Ruby, instances of the String class can be created using a literal or the new method on the String class.

irb(main):001:0> s1="Hello World"
=> "Hello World"
irb(main):002:0> s2=String.new("Hello World")
=> "Hello World"
Of course, the String defines a great many methods (and operators) for string instances. Also, strings can either be specified with single or double quotes. Double quotes allow for many escape characters and embedded expressions to be evaluated and used in the strings. With single quoted strings, what you see is what you get. For a better understanding, look at the following examples.

irb(main):001:0> str1='a \n string'
=> "a \\n string"
irb(main):002:0> str2="a \n string"
=> "a \n string"
irb(main):003:0> puts str1
a \n string
=> nil
irb(main):004:0> puts str2
=> nil
irb(main):005:0> 'try to add #{2+2}'
=> "try to add \#{2+2}"
irb(main):006:0> "try to add #{2+2}"
=> "try to add 4"
irb(main):007:0> this="that"
=> "that"
irb(main):008:0> 'when single quote rights #{this}'
=> "when single quote rights \#{this}"
irb(main):009:0> "double quote rights #{this}"
=> "double quote rights that"
Notice how text inside of double quotes is evaluated before displaying where character escapes (like \n) and expressions (like #{2+2}) included. On the other hand, these items are treated literally in single quoted strings?

Besides the single and double quote characters to denote a string literal, there is another way to write string literals in Ruby. A percent sign and lowercase or uppercase letter Q can be used to write a string in either single-quote or double-quote style respectively.

irb(main):001:0> %q@this is a single quote string #{2+2} here@
=> "this is a single quote string \#{2+2} here"
irb(main):002:0> %Q@this is a double quote string #{2+2} here@
=> "this is a double quote string 4 here"
Note that the character following q% or Q% defines the beginning and end of the string literal. The @ symbol is used in this example as the delimiter marking the beginning and ending of the string.

It should be noted for those that come from other programming language backgrounds that Ruby does not differentiate between a string and character. In other words, there is no special class for single characters, they are just small strings.

Lastly, before leaving the "standard" types arena, let's look at Booleans. In Ruby, there are two classes for Boolean: TrueClass and FalseClass. There is only one instance (a singleton) for each of these classes; namely true and false. These are global values accessible anywhere in Ruby. There is also a class called the NilClass. NilClass also has only one instance—nil. Nil represents…well, nothing. In the case of Boolean logic, however, nil is synonymous with false.

irb(main):001:0> true|false
=> true
irb(main):002:0> true&false
=> false
irb(main):003:0> true|nil
=> true
irb(main):004:0> true&nil
=> false

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date