Ruby: String no longer mixes in Enumerable in 1.9

code-formatting, ruby

Solution

It is not what an enumerable string atually enumerates. Is a string a sequence of ...

- lines,

- characters,

- codepoints or

- bytes?

The answer is: all of those, any of those, either of those or neither of those, depending on the context. Therefore, you have to tell Ruby which of those you actually want.

There are several methods in the `String` class which return enumerators for any of the above. If you want the pre-1.9 behavior, your code sample would be

'im a string meing!'.bytes.to_a.pop

This looks kind of ugly, but there is a reason for it: a string is a sequence. You are treating it as a stack. A stack is not a sequence, in fact it pretty much is the opposite of a sequence.

Problem

So how can I still be able to write beautiful code such as: ``` 'im a string meing!'.pop ``` Note: `str.chop` isn't sufficient answer

Original source