5 Replies - 3309 Views - Last Post: 12 February 2012 - 06:34 PM Rate Topic: -----

#1 muricula  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 35
  • Joined: 24-November 11

undefined method `each'

Posted 11 February 2012 - 11:44 PM

Hello,
I have been trying to implement a little RSA algorithm to get used to ruby classes. I am getting the following error in terminal:

RSA.rb:22:in `encrypt': undefined method `each' for 42:Fixnum (NoMethodError)
	from RSA.rb:50


The catch is that I never call an "each" method, and even when I was using "array.each" my resources ( like rubymonk and Pragmatic Ruby) both indicate that this is correct.
I also read that you can override the "each" method in my class, however I did not think this was necessary. Do I need to do this? If so how?
Is there a better way to write any/all of this code? Is there anything stupid or javaish in this code (like maybe all those @ signs, do I really need them all)?

If you have questions about the algorithm you can find it here
 class RSA

def initialize( pIn, qIn, eIn, msg)
@p = pIn
@q = qIn
@e = eIn

@n = (@p * @q)
@phiN = (@p-1) * (@q-1)
@d = (@e ** (-1)) % @phiN
@data = msg

end


def encrypt(plainText)

#plainText.each do |thing|
for i in plainText
plainText.push( (data**(e))% N )

end
 
print plainText
return plainText

end


def decrypt(cipherText)

#cipherText.each do |thing|
for i in cipherText
cipherText.push( (cipherText**(e))% N )

end

print cipherText
return cipherText

end


end # class

message = RSA.new(51,37,87,[ 42, 24])
var = message.encrypt(42)
message.decrypt(var)



Thanks.

*edited for copy

This post has been edited by muricula: 11 February 2012 - 11:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: undefined method `each'

#2 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 877
  • View blog
  • Posts: 3,122
  • Joined: 12-May 09

Re: undefined method `each'

Posted 11 February 2012 - 11:57 PM

I see you commented out calls to each. Here's the funny thing - you are actually calling each.

for var in collection is just syntactic sugar for collection.each do |var|

What you might want to examine is why you're attempting to do for in over a number type - that's not correct no matter how you try to call it.

This post has been edited by xclite: 13 February 2012 - 08:07 AM

Was This Post Helpful? 1
  • +
  • -

#3 muricula  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 35
  • Joined: 24-November 11

Re: undefined method `each'

Posted 12 February 2012 - 12:16 AM

I think I am trying to do for over an array of numbers. Although msg is not specified as an array in initialize (I don't think you can initialize it as msg[] like in a C style language), I do pass an array to msg when I instantiate the class. Isn't msg an array then?

Ruby seems like it is pretty prone to type errors.
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 877
  • View blog
  • Posts: 3,122
  • Joined: 12-May 09

Re: undefined method `each'

Posted 12 February 2012 - 12:51 AM

You definitely aren't passing an array in. Look at what you're doing - you call encrypt(42).
42 is a number, but your method thinks it's a collection of some kind. The first thing your encrypt method tries to do is call each on the 42.

Ruby is not prone to type errors as a language (at least any more than Python or any other dynamically typed language), it's prone to type errors because you're passing something in incorrectly in this line: var = message.encrypt(42)... plaintext gets the value of 42 that you passed in, which is not iterable.

This post has been edited by xclite: 12 February 2012 - 12:52 AM

Was This Post Helpful? 1
  • +
  • -

#5 muricula  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 35
  • Joined: 24-November 11

Re: undefined method `each'

Posted 12 February 2012 - 06:29 PM

Thank you. That was a stupid mistake, when I initialized the class the message was an array, but when I called the method I only passed a single number.
I am surprised though that fixnums aren't treated like a one element array by iterators.

This post has been edited by muricula: 12 February 2012 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: undefined method `each'

Posted 12 February 2012 - 06:34 PM

View Postmuricula, on 13 February 2012 - 02:29 AM, said:

I am surprised though that fixnums aren't treated like a one element array by iterators.


What possible use would that have other than hiding programmer mistakes? I can't think of any scenario where such behavior would be useful.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1