first(limit = 1)
public

Returns the first character of the string or the first limit characters.

Examples:

  "hello".first     # => "h"
  "hello".first(2)  # => "he"
  "hello".first(10) # => "hello"

2Notes

PS

insane-dreamer · Mar 12, 2009

That didn't render properly. Use:

String[1..1]

instead of

String.first

Conflicts with 1.8.7 (and 1.9.1)

dvdplm · Apr 1, 2009

"abc".to(0) fails in 1.8.7 and "String"[1..1] should be "String"[0..0] (or "abc"[0...1]). Using to_a breaks in 1.9.1.

I'm using "abc"[0..0]. :(