From: "Jan E." Date: 2012-05-18T05:37:23+09:00 Subject: Re: what is going wrong here? The case of the noob not understanding initialize roob noob wrote in post #1061170: > Honestly I see very little difference between > > " > message = "whatever" > encrypt(message) #returns the AES-256 encryption of message > " > > and > > " > @message > encrypt #returns the AES-256 encryption of @message > " The difference is that the first method works on its own. You don't need a certain context or specific preparations, you simply pass a string and get the encrypted string. On the other hand, the second method is completely dependend on the outer world. You cannot even call it without having set the @message variable before. And you have to make sure the value isn't changed in the meantime (which makes the whole program rather "unstable" and completely unsuitable for threaded applications). The worst thing is that these dependencies aren't even obvious from the code. You'd have to write a comment and explain which steps have to be taken. Otherwise, there will be strange errors like "nil doesn't have a + method". Well, if you only write small programs for yourself and don't plan to publish them, this all might be acceptable. But I wouldn't even start to adept this programming style, because it will get in your way as soon as your applications become bigger or you share the code with others. This is a bit like arguing about if you can use a hammer to put a screw in. Well, if you only got a few screws and the result doesn't really matter, then you *can* do it. It does work. But it's the wrong way and it isn't suitable as a general approach. By the way: Your programming style wouldn't even work in most other languages. It relies on the fact that Ruby is rather sloppy with instance variables and allows them to be created on the fly by any method. In more strict programming languages, every variable has to be declared first, which would make the whole approach rather tedious. roob noob wrote in post #1061186: > sorry for post spam, but would this be the appropriate way to do it? I > still prefer the @ way, but I want to do things right. > > class AddNumbersAndLetters > > def get_input > input = gets.chop > input = add_numbers(input) > input = add_letters(input) > input = add_letters(input) > input = add_numbers(input) > input = add_letters(input) > end > > def add_numbers(input) > input += "123" > end > > def add_letters(input) > input += "abc" > end > end Yes, this makes a lot more sense. A more advanced approach would be to only write a "rule" how the string should be built and then let the program do the actual method chaining on its own: input = gets.chop transform_input = [ :add_numbers, :add_letters, :add_letters, :add_numbers, :add_letters ] transform_input.inject input do |result, method| send method, result end See http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-inject on how the inject method works. By the way, you should replace the "+=" with a simple "+". You don't want to reassign the input parameter but simply get the result of adding "123" to input. -- Posted via http://www.ruby-forum.com/.