Search code examples
rubysplitnomethoderrorfixnum

Ruby: Private method called for 3:Fixnum


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

Solution

  • 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