是否有一个更像剁的红宝石切片?

mbi*_*ras 3 ruby

chop 切断最后一个字符并返回剩余的字符串:

irb(main):011:0> "hello world".chop
=> "hello worl"
Run Code Online (Sandbox Code Playgroud)

slice 可以用来切断第一个字符

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

是否有一个Ruby方法切断第一个字符,但然后返回字符串的其余部分?看起来就像

irb(main):011:0> "hello world".some_method
=> "ello world"
Run Code Online (Sandbox Code Playgroud)

K M*_*lam 5

以下是几种方法:

> str = "hello world"
=> "hello world"
> str[1..-1]
=> "ello world"
> str = "hello world"
=> "hello world"
> str.slice(1..-1)
=> "ello world"
Run Code Online (Sandbox Code Playgroud)