LoginSignup
0

More than 3 years have passed since last update.

【Ruby小ネタシリーズ】> ?a => "a"

Posted at

$ ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]

$ irb
> ?{
=> "{" 
> ?\s
=> " " 
> ?a
=> "a" 

> ?ab
SyntaxError: (irb):4: syntax error, unexpected '?'


$ ruby -v
ruby 1.8.7 (2008-05-31 patchlevel 0) [i686-darwin18.5.0]

$ irb
> ?a
=> 97

> ?a.class
=> Fixnum

> ?ab
SyntaxError: compile error
(irb):2: syntax error, unexpected '?'
    from (irb):2

解説

この?はRuby1.8以下ではASCIIコードをFixnumで返していたのですが、1.9からは文字列を返すようになりました。

参考

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
0