- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个方法可以获取数组或字符串
为了使该方法正常工作,我需要将该字符串转换为数组。因为它有时会得到字符串,所以我想让它检查变量是否是数组,如果不是,则将其转换为数组。所以我做了以下事情:
unless variablename.is_a?(Array)
variablename = variablename.lines.to_a
end
第二行失败,我收到一个 ruby 错误,指出“lines”对数组对象不可用。
我也试过 .kind_of?结果相同
我得到了答案,但我还想弄清楚我到底在问什么。我正在测试 variablename 是否是一个数组。由于某种原因,当 variablename 是一个数组时,它仍然运行并且第二行失败并出现以下错误: #Array:0x000000021382b8 (NoMethodError) 的未定义方法“行”
最佳答案
def do_stuff(x)
x = x.lines.to_a if x.is_a? String
x
end
data = [
"hello\nworld",
[1, 2, 3]
]
data.each do |item|
p do_stuff item
end
现在,除非:
def do_stuff(x)
unless x.is_a?(Array)
x = x.lines.to_a
end
x
end
data = [
"hello\nworld",
[1, 2, 3],
['a', 'b']
]
data.each do |item|
p do_stuff item
end
--output:--
["hello\n", "world"]
[1, 2, 3]
["a", "b"]
但是,在对象上调用 String 方法之前检查 String 对象比检查不是 Array 更有意义。
关于Ruby String 变量为 .is_a 返回 true?(Array),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18662608/
var_dump(is_a([], "array")); 上面的代码显示“false”。 但是, echo gettype([]); 显示“数组”。 我在这里做错了什么或者错过了什么吗? 最佳答案 i
var_dump(is_a([], "array")); 上面的代码显示“false”。 但是, echo gettype([]); 显示“数组”。 我在这里做错了什么或者错过了什么吗? 最佳答案 i
instanceof 运算符 和 is_a() 方法都是判断:某对象是否属于该类 或 该类是此对象的父类(用于确定一个 PHP 变量是否属于某一类 class 的实例) 是的话返回 TRUE,不是
我正在尝试使用下面的代码,仅当键是整数时才在散列中保留一个值。我在这里做错了什么? hackerrank.keep_if do |x,y| x.is_a? Integer end 编辑:我一定是做错了
我想知道是否存在与 Ruby 的“is_a”等效的 Python?方法? > "".is_a? String => true Info: is_a?(class) → true or false Re
ruby 1.8.7 (2010-12-23 补丁级别 330) [i686-darwin10.5.0] 我错过了什么吗? module Mixin def is_a?(o) return
简单的例子: class A end class B < A end 那么,如何判断B类是否继承自A类呢?是否有某种类似于 is_a? 或称为 is_child_of? 的方法? 我找不到。 最佳答案
我正在尝试使用 Ruby 并尝试创建一个小型银行账户程序。当我运行这行与 create_account 一起运行的特定代码时: unless @response.is_a? Integer && @r
如何检查给定参数是否为 lambda? def method(parameter) if ??? puts "We got lambda" parameter.call
我知道 instanceof是一个运算符,而 is_a是一种方法。 该方法的性能是否较慢?你更喜欢用什么? 最佳答案 更新 截至 PHP 5.3.9 ,is_a() 的功能发生了变化。下面的原始答案指
我有三个属于同一个类的对象。一个是通过 Item.new 创建的,另外两个是从数据库 (Mongoid) 中提取的。我将这些对象中的一个/任何一个传递给另一个方法,并通过 is_a? 检查该方法中的类
有什么区别?我什么时候应该使用哪个?为什么有这么多? 最佳答案 kind_of? 和 is_a? 是同义词。 instance_of? 与其他两个的不同之处在于它仅在对象是该类的实例而不是子类的实例时
我有一个方法可以获取数组或字符串 为了使该方法正常工作,我需要将该字符串转换为数组。因为它有时会得到字符串,所以我想让它检查变量是否是数组,如果不是,则将其转换为数组。所以我做了以下事情: unles
我才学习 Ruby 的深层部分才几个月,如果这是一个有点愚蠢的问题,我深表歉意。我正在尝试递归迭代可能 包含嵌套数组的数组,因此我需要检查当前元素的类型。我有以下代码作为小测试: arr = [ 1.
我正在使用cakephp 2.0。我已经将用户登录名与xenforo集成在一起,但是在进行诸如添加,编辑,删除之类的任何操作时,进行整合后, View 无法重定向,$this->redirect无法正
我想要这个代码: [1,2,3].all? {|x| x.is_a?(Integer)} 使用 &: 方法工作,例如: [1,2,3].all?(&:is_a?(Integer)) 但是我得到这个错误
我是一名 Python 开发人员,刚开始学习 Rails,所以这可能是一个菜鸟问题。我在我的代码中发现了一些令人惊讶的行为: 这会导致以下错误:未定义的方法“空?”对于 nil:NilClass
我想遍历 QML 组件列表并选择一种类型: for (var i = 0; i < controls.children.length; ++i) { if ( typeof (control
我正在使用 Rails 2.3.9。为了说明这个问题,假设我们在模型上有一个命名范围: class Book 'isbn IS NOT NULL' end 如果你得到了命名范围的类,ActiveRe
我用谷歌搜索发现有一个 is_a? 函数可以检查对象是否为整数。 但我在 Rails 控制台中尝试过,但它不起作用。 我运行了如下代码: "1".is_a? 1.is_a? 我错过了什么吗? 最佳
我是一名优秀的程序员,十分优秀!