<p>I have a string, say <code>'123'</code>, and I want to convert it to the integer <code>123</code>.</p>

<p>I know you can simply do <code>some_string.to_i</code>, but that converts <code>'lolipops'</code> to <code>0</code>, which is not the effect I have in mind. I want it to blow up in my face when I try to convert something invalid, with a nice and painful <code>Exception</code>. Otherwise, I can't distinguish between a valid <code>0</code> and something that just isn't a number at all.</p>

<p><strong>EDIT:</strong> I was looking for the standard way of doing it, without regex trickery.</p>

Ruby has this functionality built in:
```
```
Integer('1001')                                    # => 1001  
Integer('1001 nights')  
# ArgumentError: invalid value for Integer: "1001 nights"
```
```
As noted in answer by
Joseph Pecoraro
, you might want to watch for strings that are valid non-decimal numbers, such as those starting with
```
0x
```
for hex and
```
0b
```
for binary, and potentially more tricky numbers starting with zero that will be parsed as octal.
Ruby 1.9.2 added optional second argument for radix so above issue can be avoided:
```
```
Integer('23')                                     # => 23
Integer('0x23')                                   # => 35
Integer('023')                                    # => 19
Integer('0x23', 10)
# => #<ArgumentError: invalid value for Integer: "0x23">
Integer('023', 10)                                # => 23
```
```