图1.Ruby的数字类型类继承图。 |
irb(main):001:0> 3.class => Fixnum irb(main):002:0> 3.4.class => Float irb(main):003:0> 10000000000000000000.class => Bignum |
< irb(main):001:0> 3.14E5 #指数标志 => 314000.0 irb(main):002:0> 054 #八进制 => 44 irb(main):003:0> 0x5A #十六进制 => 90 irb(main):004:0> 0b1011 #二进制 => 11 irb(main):005:0> 10_000 #10,000,用下划线隔开 => 10000 irb(main):006:0> i=10,000 #创建一个数组而不是10000 Fixnum => [10, 0] irb(main):007:0> i.class => Array irb(main):008:0> ?Z #Fixnum ASCII值 => 90 irb(main):009:0> ?Z.class => Fixnum irb(main):010:0> ?\C-s #Control-s的值ASCII => 19 |
irb(main):001:0> i=4 => 4 irb(main):002:0> i.class => Fixnum irb(main):003:0> i=i+100000000000000 => 100000000000004 irb(main):004:0> i.class => Bignum irb(main):005:0> i=i-100000000000000 => 4 irb(main):006:0> i.class => Fixnum |
irb(main):001:0> s1="Hello World" => "Hello World" irb(main):002:0> s2=String.new("Hello World") => "Hello World" |
irb(main):001:0> str1='a \n string' => "a \\n string" irb(main):002:0> str2="a \n string" => "a \n string" irb(main):003:0> puts str1 a \n string => nil irb(main):004:0> puts str2 a string => nil irb(main):005:0> 'try to add #{2+2}' => "try to add \#{2+2}" irb(main):006:0> "try to add #{2+2}" => "try to add 4" irb(main):007:0> this="that" => "that" irb(main):008:0> 'when single quote rights #{this}' => "when single quote rights \#{this}" irb(main):009:0> "double quote rights #{this}" => "double quote rights that" |
irb(main):001:0> %q@this is a single quote string #{2+2} here@ => "this is a single quote string \#{2+2} here" irb(main):002:0> %Q@this is a double quote string #{2+2} here@ => "this is a double quote string 4 here" |