LoginSignup
3
3

More than 5 years have passed since last update.

Ruby1.8系と1.9系以降での文字列型の扱いの違い

Posted at

移植しようとしてビックリした仕様変更。

text = '1000'
puts text[0]     # => 49    1.8.7
puts text[0]     # => "1"   1.9以降

1.9以降では文字そのものを返すが、1.8系では文字コード("1"の文字コードは49)を返す。

ちなみに回避策はこちら。

text = '1000'
puts text[0,1]     # => "1"    1.8.7
puts text[0,1]     # => "1"    1.9以降

PHPのsubstrっぽい書式。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3