I Have a Ruby String ...
Tags: ruby, string
Published: 2019-05-17 14:32:31

---
title: I Have a Ruby String ...
published: true
description: 
tags: ruby, string
---
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, <code>irb</code>.  

Each example:

- Shows a string.
- Performs some operation on the string.
- Shows the result.

<h3>I Have a Ruby String, But What I Want Is ...

<h4>A Class</h4>

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

<h4>An Instance of a Class</h4>

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

<h4>A Symbol</h4>

This is easy.

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

Also:

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

<h4>An Integer</h4>

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

Interpreted in a different base:

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

Interpreted as octal:

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

Interpreted as hexadecimal:

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

<h4>A Float</h4>

```ruby
s
# => "1.5"
f = s.to_f
# => 1.5
f.class
# => Float
```
With an exponent:

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

<h4>A Rational</h4>

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

<h4>A Complex</h4>

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

<h4>An Array of Bytes</h4>

```ruby
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
```

<h4>An Array of Characters</h4>

```ruby
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
```

<h4>An Array of Codepoints</h4>

```ruby
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
```

<h4>An Array of Lines</h4>

```ruby
s
# => "Twas brillig, and the slithy toves\n      Did gyre and gimble in the wabe:\nAll mimsy were the borogoves,\n      And the mome raths outgrabe. \n"
l = s.lines
# => ["Twas brillig, and the slithy toves\n", "      Did gyre and gimble in the wabe:\n", "All mimsy were the borogoves,\n", "      And the mome raths outgrabe. \n"]
l.class
# => Array
```

<h4>An Array From Split</h4>

```ruby
s
# => "Twas brillig, and the slithy toves\n      Did gyre and gimble in the wabe:\nAll mimsy were the borogoves,\n      And the mome raths outgrabe. \n"
a = s.split("\n")
# => ["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
```

<h4>An Enumerator for the Bytes</h4>

```ruby
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"
```

<h4>An Enumerator for the Characters</h4>

```ruby
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"
```

<h4>An Enumerator for the Codepoints</h4>

```ruby
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]
```

<h4>An Enumerator for the Lines</h4>

```ruby
s
# => "Twas brillig, and the slithy toves\n      Did gyre and gimble in the wabe:\nAll mimsy were the borogoves,\n      And the mome raths outgrabe. \n"
enum = s.each_line
# => #<Enumerator: "Twas brillig, and the slithy toves\n      Did gyre and gimble in the wabe:\nAll mimsy were the borogoves,\n      And the mome raths outgrabe. \n":each_line>
enum.class
# => Enumerator
enum.each do |line|
    puts line
  end
Twas brillig, and the slithy toves
      Did gyre and gimble in the wabe:
All mimsy were the borogoves,
      And the mome raths outgrabe.
# => "Twas brillig, and the slithy toves\n      Did gyre and gimble in the wabe:\nAll mimsy were the borogoves,\n      And the mome raths outgrabe. \n"
```