4

I have tried to partition a string using the partition method from the String module. However, when doing so:

puts "test".partition("s")

I get the following error message:

Line 1:in `partition': wrong number of arguments (1 for 0) (ArgumentError) from t.rb:1

I believe that Ruby calls the partition method from the Enumerable module, instead of the one from the String module as I wanted.

How can I get Ruby to call the desired method?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Jan-Willem
  • 187
  • 1
  • 12
  • Which version/platform of Ruby are you using? – Andy Waite Jun 29 '11 at 09:38
  • Can you put the output of the following in irb: >> RUBY_VERSION >> "test".partition("s") If you're using Ruby 1.8.6, String#partition doesn't do what you think – Lee Jarvis Jun 29 '11 at 09:40
  • I use ruby 1.8.4 (2005-12-24) [i686-linux] – Jan-Willem Jun 29 '11 at 09:43
  • 6
    @Jan that's why it's not working, you're viewing documentation for 1.8.7 or 1.9.2. You're using an ancient version of Ruby. I recommend upgrading to either 1.8.7 or 1.9.2 – Lee Jarvis Jun 29 '11 at 09:43
  • irb(main):001:0> RUBY_VERSION => "1.8.4" irb(main):002:0> "test".partition("s") ArgumentError: wrong number of arguments (1 for 0) from (irb):2:in `partition' from (irb):2 – Jan-Willem Jun 29 '11 at 09:46
  • I once came across a similar issue in Rubinius: https://github.com/rubinius/rubinius/issues/1005 – Andrew Grimm Jun 29 '11 at 11:52
  • I tripped over this problem (reading the wrong docs) recently in Regex#match and String#match, which take an optional second parameter (offset) in 1.9.3. The search was compounded by my text editor starting rspec with ruby 1.8.7, while my tests in irb were using 1.9.3. :-) – sheldonh Nov 07 '11 at 20:35

1 Answers1

2

As injekt has pointed out, 1.8.4 is too old. APIdock shows that String#partition only appears in 1.8.7. Upgrade.

Community
  • 1
  • 1
Martin Vidner
  • 2,307
  • 16
  • 31