외부 시스템과 통신하기 위해 MIME 유형을 캡슐화하는 클래스가 있습니다. 여기에 은 문제와 관련된 클래스의 일부입니다.String 값 유형을 검사 할 때 이상한 결과가 발생합니다.
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
으로 시도했지만 false가 인쇄 되었습니까?
내가 puts "tmp".is_a? String
을하려고 할 때 내가 기대되는 사실이 ...
코드는 INT, 플로트, 부울, 해시와 함께 작동,하지만 난 그것을 문자열로 동작하지 않습니다와 나는하지 않습니다 오류를 참조하십시오.
나는 그 문제를 해결할 수 없으며 모든 도움을 주시면 대단히 감사하겠습니다.
감사합니다.
"simothing", "bu"란 무엇입니까? "보시다시피", "i", "... ???", "Thx"는 누군가에게 물어 보는 올바른 모드라고 생각하지 않습니다. – sawa
나는 나의 질문을 재구성하려고 노력할 것이다 - 나의 영어는 그렇게 좋지 않다. 그러나 나는 최선을 다할 것이다;) – zedryas
당신은 정확하게 무엇을하려하고 있냐? 나는 더 좋은 방법이 있다는 것을 거의 확신한다. – Agis