Sometimes I have a Ruby string, but I'd like to have something else that's based on the string.

Here are some code examples around that.

Some of these techniques would not be needed if I know exactly what's in the string, but sometimes I don't know. Example: parsing.

Everyone will know some of these techniques, and some may know all of them. But:

"People need to be reminded more often than they need to be instructed." -- Dr. Samuel Johnson

The examples are coded in the interactive Ruby Shell, irb .

Each example:

Shows a string.

Performs some operation on the string.

Shows the result.

I Have a Ruby String, But What I Want Is ... A Class

s # => "Hash" c = Object . const_get ( s ) # => Hash c . class # => Class

An Instance of a Class

s # => "Hash" h = Object . const_get ( s ). new # => {} h . class # => Hash

A Symbol

This is easy.



s # => "my string" sym = s . to_sym # => :"my string" sym . class # => Symbol

Also:



s # => "my string" sym = s . intern # => :"my string" sym . class # => Symbol

An Integer

s # => "100" i = s . to_i # => 100 i . class # => Integer

Interpreted in a different base:



s # => "111" i = s . to_i ( 2 ) # => 7 i . class # => Integer

Interpreted as octal:



s # => "0246" o = s . oct # => 166 o . class # => Integer

Interpreted as hexadecimal:



s # => "0xdeadbeef" h = s . hex # => 3735928559 h . class # => Integer

A Float

s # => "1.5" f = s . to_f # => 1.5 f . class # => Float

With an exponent:



s # => "0.314159e1" f = s . to_f # => 3.14159 f . class # => Float

A Rational

s # => "10/4" r = s . to_r # => (5/2) r . class # => Rational

A Complex

s # => "2+2i" c = s . to_c # => (2+2i) c . class # => Complex

An Array of Bytes

s # => "Do you feel lucky?" b = s . bytes # => [68, 111, 32, 121, 111, 117, 32, 102, 101, 101, 108, 32, 108, 117, 99, 107, 121, 63] b . class # => Array

An Array of Characters

s # => "Do you feel lucky?" a = s . chars # => ["D", "o", " ", "y", "o", "u", " ", "f", "e", "e", "l", " ", "l", "u", "c", "k", "y", "?"] a . class # => Array

An Array of Codepoints

s # => "Do you feel lucky?" c = s . codepoints # => [68, 111, 32, 121, 111, 117, 32, 102, 101, 101, 108, 32, 108, 117, 99, 107, 121, 63] c . class # => Array

An Array of Lines

s # => "Twas brillig, and the slithy toves

Did gyre and gimble in the wabe:

All mimsy were the borogoves,

And the mome raths outgrabe.

" l = s . lines # => ["Twas brillig, and the slithy toves

", " Did gyre and gimble in the wabe:

", "All mimsy were the borogoves,

", " And the mome raths outgrabe.

"] l . class # => Array

An Array From Split

s # => "Twas brillig, and the slithy toves

Did gyre and gimble in the wabe:

All mimsy were the borogoves,

And the mome raths outgrabe.

" a = s . split ( "

" ) # => ["Twas brillig, and the slithy toves", " Did gyre and gimble in the wabe:", "All mimsy were the borogoves,", " And the mome raths outgrabe. "] a . class # => Array

An Enumerator for the Bytes

s # => "xyzzy" enum = s . each_byte # => #<Enumerator: "xyzzy":each_byte> enum . class # => Enumerator enum . each do | b | p b end 120 121 122 122 121 # => "xyzzy"

An Enumerator for the Characters

s # => "xyzzy" enum = s . each_char # => #<Enumerator: "xyzzy":each_char> enum . class # => Enumerator enum . each do | char | puts char end x y z z y # => "xyzzy"

An Enumerator for the Codepoints

s # => "xyzzy" enum = s . codepoints # => [120, 121, 122, 122, 121] enum . class # => Array enum . each do | codepoint | puts codepoint end 120 121 122 122 121 # => [120, 121, 122, 122, 121]

An Enumerator for the Lines