mbigras mbigras - 6 months ago 16
Ruby Question

Is there a Ruby slice that's more like a chop?

chop
cuts off the last character and returns the remaining string:

irb(main):011:0> "hello world".chop
=> "hello worl"


slice
can be used to cut off the first character

irb(main):013:0> str = "hello world"
=> "hello world"
irb(main):014:0> str.slice!(0)
=> "h"
irb(main):015:0> str
=> "ello world"


Is there a Ruby method that cuts off the first character but then returns the rest of the string? So it would look like

irb(main):011:0> "hello world".some_method
=> "ello world"

Answer

Here are couple of ways:

> str = "hello world"
=> "hello world"
> str[1..-1]
=> "ello world"
> str = "hello world"
=> "hello world"
> str.slice(1..-1)
=> "ello world"