我有一個類來封裝用於與外部系統進行通信的MIME類型。 這裏是與該問題相關的類的一部分。嘗試檢查時出現奇怪的結果字符串值類型
module Types
class MetaType
def self.validator(&block)
define_singleton_method :is_valid?, &block
end
end
class Float < MetaType
validator { |v| v.is_a? Numeric }
end
class String < MetaType
validator { |v| v.is_a? String }
end
Map = { :Time => Float, :Float => Float , :String => String }
def self.get_type(name)
name = name.intern
raise ArgumentError, "Unknown type #{name}" unless Map.has_key? name
return Map[name]
end
end
這裏是規格
describe Types do
context "When calling get_type with 'Float'" do
subject { Types.get_type('Float') }
it "should validate a float" do
expect(subject.is_valid? 3.5).to be_true
end
end
context "When calling get_type with 'String'" do
subject { Types.get_type('String') }
it "should validate a string" do
expect(subject.is_valid? "tmp").to be_true
end
end
end
規範
Types
When calling get_type with 'Float'
should validate a float
When calling get_type with 'String'
should validate a string (FAILED - 1)
Failures:
1) Types When calling get_type with 'String' should validate a string
Failure/Error: expect(subject.is_valid? "tmp").to be_true
expected: true value
got: false
# ./tmp/type_error.rb:37:in `block (3 levels) in <top (required)>'
代碼不會通爲串的輸出。
我已經在元類的驗證函數內嘗試過puts val.is_a? String
,但確實打印出錯誤了嗎?
當我嘗試puts "tmp".is_a? String
我有真正的這是我期待...
代碼工作與整數,浮點,布爾,哈希,但我不能讓它使用字符串工作,我不看到任何錯誤。
我無法繞過這個問題,任何幫助將不勝感激。
謝謝
什麼是「simothing」,「不」?我不認爲「正如你所看到的」,「我」,「...... ???」,「Thx」是向某人提問的正確模式。 – sawa
我會嘗試重新說明我的問題 - 我的英語不太好,但我會盡我所能;) – zedryas
你究竟在做什麼?我幾乎可以確定有更好的方法。 – Agis