4 Replies - 737 Views - Last Post: 26 February 2014 - 06:56 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Argument error

Posted 25 February 2014 - 04:05 PM

I am trying to created an interactive program in the following code. I don't understand why the method call #test on line 4 is resulting in an argument error. Test can't even accept arguments as far as I know. In addition line 33 is an attempt to start the whole program over if the the inputed password is invalid.

class Password
		puts "Please make a password that is 6 to 20 chars in length, conatains a number or special char and has an uppercase letter."
		@string = gets.chomp
		test
	def right_length?
		if (/.{6,20}/ === @string)
			true
		else
			puts "The password needs to be 6 to 20 characters in length."
		end
	end

	def contains_uppercase?
	    if (/[A-Z]/ === @string)
	    	true
	    else
	    	puts "The password needs to contain an uppercase letter."
	    end
	end

	def contains_number_special_char? # use an || statement here
        if (/[0-9!@#$%&*+:?]/) === @string
        	true
        else
        	puts "The password needs to contain a number or special charcter."
        end
	end

	def test  
		if right_length? && contains_uppercase? && contains_number_special_char?
			puts "This is a valid password!"
		else
			try_again = Password.new
		end
	end
end

password1 = Password.new 


This post has been edited by heaphyg: 25 February 2014 - 04:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Argument error

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 894
  • View blog
  • Posts: 3,153
  • Joined: 12-May 09

Re: Argument error

Posted 25 February 2014 - 04:07 PM

Please post the exact error message.

Additionally, I don't think lines 2-4 can work as you intend anyway. Why are they inside the class, but not inside a method?
Was This Post Helpful? 2
  • +
  • -

#3 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: Argument error

Posted 25 February 2014 - 09:09 PM

pass3.rb:4:in `test': wrong number of arguments (0 for 2..3) (ArgumentError)
from pass3.rb:4:in `<class:Password>'
from pass3.rb:1:in `<main>'

I didn't put lines 2-4 in a method because I thought that putting them in a method would prevent them from automatically happening when I called the ruby file in the console. If that makes sense.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: Argument error

Posted 26 February 2014 - 08:23 AM

Since those lines aren't inside a method, they are executed in the context of the class once when the class definition is read. So they're not executed every time an object of that class is created (for that you'd need to put them inside the initialize method, though putting IO code in there is generally seen as bad practice) and they're not executed in the context of an object. Therefore they can't call instance methods.

So since your test method is an instance method, you can't call it. The reason that you get an ArgumentError and not a NoMethodError is that ruby already has a test method in the Kernel module, so Ruby thinks that's the one you're trying to call. And since that method needs at least 2 arguments, you get that error message. If you named your method something else you'd get a NoMethodError.
Was This Post Helpful? 3
  • +
  • -

#5 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: Argument error

Posted 26 February 2014 - 06:56 PM

I see. Thanks for the tip.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1