Ruby String


This post is addition to previous post Ruby Symbols and Strings more specific to Ruby string methods That’s useful to our regular coding practice.

String

“A string is a group of characters that typically make up human readable words or sentences. Because strings are essentially the mechanism by which applications communicate with their users it is inevitable that string manipulation is a key part of programming”.

1. Freezing Objects

The freeze method in class Object prevents you from changing an object, effectively turning an object into a constant. After we freeze an object, an attempt to modify it results in TypeError. For details you can refer previous post Ruby Strings and Symbols point 8th.

1.9.2p320 :277 > test = “This is ruby string”
=> “This is ruby string”
1.9.2p320 :278 > test.upcase!
=> “THIS IS RUBY STRING”
1.9.2p320 :280 > test.freeze
=> “THIS IS RUBY STRING”
1.9.2p320 :281 > test.downcase!
RuntimeError: can’t modify frozen string
from (irb#1):281:in `downcase!’
from (irb#1):281

2. frozen?

A method frozen? tells you whether an object is frozen or not.

1.9.2p320 :001 > a = “This is String”
=> “This is String”
1.9.2p320 :002 > a.frozen?
=> false
1.9.2p320 :004 > a.freeze
=> “This is String”
1.9.2p320 :005 > a.frozen?
=> true

3. Comparing two strings for equality

Below are the three frequent used methods.

1.9.2p320 :011 >   a = “I am ruby developer”
=> “I am ruby developer”
1.9.2p320 :012 > b = “I am rails developer”
=> “I am rails developer”

a). ==

The most common one is == (double equals sign).

1.9.2p320 :016 >   a == b
=> false

b).String.eql?

Tests two strings for identical content. It returns the same result as ==.

1.9.2p320 :018 > a.eql?(b)
=> false

c).String.equal?

Tests whether two strings are the same object.

1.9.2p320 :019 > a.equal?(b)
=> false

4.Parse lines in a String object with #each_line or #lines, instead of #each

1.9.2p320 :056 >   s1 = “Mutable objects can be changed after assignment while immutable objects can only be overwritten”
=> “Mutable objects can be changed after assignment while immutable objects can only be overwritten”
1.9.2p320 :057 > s1.each {|a| p a}
NoMethodError: undefined method `each’ for #<String:0x92c1cf4>
from (irb):57
from /usr/local/rvm/rubies/ruby-1.9.2-p320/bin/irb:16:in `<main>’
1.9.2p320 :058 > s1.each_line {|a| p a}
“Mutable objects can be changed after assignment while immutable objects can only be overwritten”
=> “Mutable objects can be changed after assignment while immutable objects can only be overwritten”
1.9.2p320 :059 > s1.lines {|a| p a}
“Mutable objects can be changed after assignment while immutable objects can only be overwritten”
=> “Mutable objects can be changed after assignment while immutable objects can only be overwritten”

5. String#ord returns the UTF-8 index

1.9.2p320 :069 > a = ‘A’
=> “A”
1.9.2p320 :070 > b = “c”
=> “c”
1.9.2p320 :071 > c = “ABC”
=> “ABC”
1.9.2p320 :072 > a.ord
=> 65
1.9.2p320 :073 > b.ord
=> 99
1.9.2p320 :074 > c.ord
=> 65

6. String#codepoints to get an enumerator of each codepoint (UTF-8 index)

1.9.2p320 :081 > s1.codepoints.each {|a| p “#{a}”}
“77”
“117”
“116”
“97”
“98”
“108”
“101”
“32”
=> “Mutable”

7.String#clear clears to an empty string in place.

1.9.2p320 :094 >   s1
=> “Mutable objects can be changed after assignment while immutable objects can only be overwritten”
1.9.2p320 :095 > s1.clear
=> “”

8. String#length returns length in chars.

1.9.2p320 :100 >   s1.length
=> 95

9. String#encoding returns the relevant Encoding object for the string.

1.9.2p320 :102 > s1.encoding
=> #<Encoding:UTF-8>

Here just I limelight few important and new feature that are part of ruby 1.9.3 🙂