问题
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?
Here is my code:
DATA = [3, 4, 5, 6, 7, 8]
DATA.each do |line|
vals = line.split
print vals[0] + vals[1], " "
end
Here is the error message I get when I run this in IRB:
NoMethodError: private method `split' called for 3:Fixnum
回答1:
You are calling the method split
on a number object — this method exists in the String
class but not in Fixnum
.
I think what you're trying to do is this:
DATA = ['3,4', '5,6', '7,8']
DATA.each do |val|
vals = line.split ','
print vals[0] + vals[1], " "
end
回答2:
It's not clear what you are trying to do. You don't have a "line", you have an element from the Array that is an Integer value. Split
is a method defined on String.
DATA = [3, 4, 5, 6, 7, 8]
DATA.each do |val|
print val
end
Now, the reason you get that confusing error message is a little bit interesting. As it happens, there is a Kernel#split
which allows a Perl-like brevity for scripts. It splits the global variable $_
which holds the last result from gets
.
>> gets
how now brown cow
=> "how now brown cow\n"
>> split ' '
=> ["how", "now", "brown", "cow"]
>> puts $_
how now brown cow
But being mixed in to Object, so it is available at the script level, leaves the problem that every single class ever created ends up having a #split
method.
If you run plain old split() every object in the system will respond, but fortunately only with an error.
By making it a private method, it's available at the top level for scripts (because "Object" is open or something) but doesn't end up part of the API of every single instantiated object.
回答3:
Guessing at what you might really want:
DATA = 3..8
DATA.each_cons(2) do |a,b|
puts "#{a} + #{b} = #{a+b}"
end
#=> 3 + 4 = 7
#=> 4 + 5 = 9
#=> 5 + 6 = 11
#=> 6 + 7 = 13
#=> 7 + 8 = 15
Edit: Based on the desire to operate on successive pairs:
DATA = 3..8 # or DATA = [3,4,5,6,7,8]
DATA.each_slice(2) do |a,b|
puts "#{a} + #{b} = #{a+b}"
end
#=> 3 + 4 = 7
#=> 5 + 6 = 11
#=> 7 + 8 = 15
来源:https://stackoverflow.com/questions/5697282/ruby-private-method-called-for-3fixnum