a = "hello" puts a[4,1] # => o puts a[5,1] # => "" puts a[6,1] # => nil
请问大佬们为啥 a[5,1] 是 "" 呢?
https://rubyapi.org/2.7/o/string#method-i-5B-5D
Additionally, an empty string is returned when the starting index for a character range is at the end of the string.
感谢大佬
牛皮啊,才知道还可以这么用
ruby 3.2 a[5,1]就是nil了
a[5,1]
nil
➜ ~ ruby --version ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-linux] ➜ ~ irb irb(main):001:0> a = "hello" => "hello" irb(main):002:0> a[5,1] => ""