Ruby - How to select some characters from string

user1946705 picture user1946705 · Jun 21, 2011 · Viewed 78.5k times · Source

I am trying to find a function for select e.g. first 100 chars of the string. In PHP, there exists the substr function

Does Ruby have some similar function?

Answer

Sorrow picture Sorrow · Jun 21, 2011

Try foo[0...100], any range will do. Ranges can also go negative. It is well explained in the documentation of Ruby.