爲什麼不能這樣做?String(42)在Ruby中做什麼?
>> s = String
>> s(42)
s(42)
NoMethodError: undefined method `s' for main:Object
from (irb):86
from /home/sam/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
下一頁。
>> String.new 42
String.new 42
TypeError: can't convert Fixnum into String
from (irb):90:in `initialize'
from (irb):90:in `new'
from (irb):90
from /home/sam/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
如果String.new不能String()將Fixnum轉換爲String?我假設String()調用to_s。但是,除了要複製的字符串之外,String.new還在尋找什麼?新的別名是dup嗎?
你的兩個問題與彼此無關。請將它們分成兩個問題。 – 2011-02-04 04:41:17