Ruby:调用3:Fixnum的私有方法 [英] Ruby: Private method called for 3:Fixnum

查看:141
本文介绍了Ruby:调用3:Fixnum的私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习此简单功能的细微差别,但不确定如何解决此NoMethodError问题.如何将拆分"设置为公开而非私有?这样可以解决问题吗?

I am trying to learn the nuances of this simple function but am not sure what I need to do to fix this NoMethodError. How do I make 'split' public rather than private? Will that fix the problem?

这是我的代码:

DATA = [3, 4, 5, 6, 7, 8]
DATA.each do |line|
vals = line.split
print vals[0] + vals[1], " "
end

这是我在IRB中运行此错误消息:

Here is the error message I get when I run this in IRB:

NoMethodError: private method `split' called for 3:Fixnum

推荐答案

您正在数字对象上调用方法split-该方法存在于String类中,但不存在于Fixnum中.

You are calling the method split on a number object — this method exists in the String class but not in Fixnum.

我认为您要这样做的是

DATA = ['3,4', '5,6', '7,8']
DATA.each do |val|
  vals = line.split ','
  print vals[0] + vals[1], " "
end

这篇关于Ruby:调用3:Fixnum的私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆