开发者

what is "?" in ruby

开发者 https://www.devze.com 2023-02-20 00:27 出处:网络
Ruby 1.9 irb(main):001:0> ?c => \"c\" Ruby 1.8.6 开发者_开发百科 irb(main):001:0> ?c => 99

Ruby 1.9

irb(main):001:0> ?c
=> "c"

Ruby 1.8.6

开发者_开发百科
 irb(main):001:0> ?c
 => 99

what does "?" mean ?


It denotes a "character". In ruby 1.8, this was represented by the ascii-code of the character. In Ruby 1.9, it's a single-character String.


In 1.8 they give you the ASCII value of a character, in 1.9 they are character literals:

>> RUBY_VERSION #=> "1.8.7"
>> ?a #=> 97 
>> RUBY_VERSION #=> "1.9.2"
>> ?a #=> "a"
>> *[?a..?c] #=> ["a", "b", "c"]


ruby-1.9.2-p0 > ?c == "c"
 => true 
0

精彩评论

暂无评论...
验证码 换一张
取 消