Ruby 解释变量 is_a?
Ruby interpreted variables is_a?
我想根据另一个变量中保存的值来检查变量的类型,但我正在努力解决这个问题。我是 ruby 的新手,但谁能告诉我如何在表达式中解释变量的值?我当前的代码如下:-
if variable.is_a?("#{variable_type}")
puts variable
end
其中 variable
可以包含任何内容,而 variable_type
包含变量的类型,例如 String 或 Fixnum。但目前这段代码给了我 TypeError: Class or module required.
任何想法?
举个小例子:
variable = 1
variable_type = String
puts variable if variable.is_a?(variable_type)
#=> nil
variable_type = Integer
puts variable if variable.is_a?(variable_type)
#=> 1
或者当您的 variable_type
是一个字符串时:
variable_type = 'Integer'
puts variable if variable.is_a?(Object.const_get(variable_type))
#=> 1
TypeError: Class or module required
这意味着,要使用 is_a?
varibale_type
应该持有一个 class 名称(任何)。
因此,如果您在 variable_type
中持有除 class 名称以外的任何其他内容,则会出现此错误。
a = :a
variable_type = Symbol
a if a.is_a? variable_type
# => :a
如果变量类型是字符串,则必须使用Module#const_get
:
variable_type = 'Symbol'
a if a.is_a? Object.const_get(variable_type)
# => :a
您的代码将一个字符串对象发送到 #is_a?
方法,而 #is_a
方法需要一个 Class.
例如,String
与 "String"
:
variable = "Hello!"
variable_type = String
"#{variable_type}" # => "String"
# your code:
if variable.is_a?("#{variable_type}")
puts variable
end
#is_a?
expects the actual Class (String, Fixnum, etc') - as you can see in the documentation for #is_a?
.
您可以通过两种方式调整代码:
传递 Class,不带字符串。
使用 Module.const_get
.
将字符串转换为 class
这里有一个例子:
variable = "Hello!"
variable_type = String
"#{variable_type}" # => "String"
# passing the actual class:
if variable.is_a?(variable_type)
puts variable
end
# or,
# converting the string to a the type:
if variable.is_a?( Module.const_get( variable_type.to_s ) )
puts variable
end
我想根据另一个变量中保存的值来检查变量的类型,但我正在努力解决这个问题。我是 ruby 的新手,但谁能告诉我如何在表达式中解释变量的值?我当前的代码如下:-
if variable.is_a?("#{variable_type}")
puts variable
end
其中 variable
可以包含任何内容,而 variable_type
包含变量的类型,例如 String 或 Fixnum。但目前这段代码给了我 TypeError: Class or module required.
任何想法?
举个小例子:
variable = 1
variable_type = String
puts variable if variable.is_a?(variable_type)
#=> nil
variable_type = Integer
puts variable if variable.is_a?(variable_type)
#=> 1
或者当您的 variable_type
是一个字符串时:
variable_type = 'Integer'
puts variable if variable.is_a?(Object.const_get(variable_type))
#=> 1
TypeError: Class or module required
这意味着,要使用 is_a?
varibale_type
应该持有一个 class 名称(任何)。
因此,如果您在 variable_type
中持有除 class 名称以外的任何其他内容,则会出现此错误。
a = :a
variable_type = Symbol
a if a.is_a? variable_type
# => :a
如果变量类型是字符串,则必须使用Module#const_get
:
variable_type = 'Symbol'
a if a.is_a? Object.const_get(variable_type)
# => :a
您的代码将一个字符串对象发送到 #is_a?
方法,而 #is_a
方法需要一个 Class.
例如,String
与 "String"
:
variable = "Hello!"
variable_type = String
"#{variable_type}" # => "String"
# your code:
if variable.is_a?("#{variable_type}")
puts variable
end
#is_a?
expects the actual Class (String, Fixnum, etc') - as you can see in the documentation for #is_a?
.
您可以通过两种方式调整代码:
传递 Class,不带字符串。
使用
Module.const_get
. 将字符串转换为 class
这里有一个例子:
variable = "Hello!"
variable_type = String
"#{variable_type}" # => "String"
# passing the actual class:
if variable.is_a?(variable_type)
puts variable
end
# or,
# converting the string to a the type:
if variable.is_a?( Module.const_get( variable_type.to_s ) )
puts variable
end