<snip code>
Anyway, I am lost on why i have to create a copy of the string in
decrypt. I know why my it string gets crushed because encrypt uses it.
How does one make it use a copy of the varablie instead of pointing to
the memory location?
As you seem to already know, all variables are just references to
objects.
In order to create a copy of a variable you can usually use .dup or
.clone.
See:
http://www.ruby-doc.org/core/classes/Singleton.html#M001467 and
class Object - RDoc Documentation.
Notice however in your programs that the line:
data= data.upcase
In encrypt actually creates a local variable named data, overshadowing
the parameter data. You can see this in IRB:
irb(main):001:0> def foo(data)
irb(main):002:1> data = data.upcase
irb(main):003:1> end
=> nil
irb(main):004:0> s = "hello"
=> "hello"
irb(main):005:0> foo s
=> "HELLO"
irb(main):006:0> s
=> "hello"
also how do you return a value in a function(def)?
You can either use the 'return' statement, or just the last value of the
method.
It is a convention to write in the style:
def foo(arg)
result =
... # calculations with arg and result
result
End
thus
ary = foo(123)
Will give the result of foo in the variable ary.
The 'return' statement is normally used when you need to prematurely
exit the method:
def foo
... #stuff
if condition
return false
end
... # more stuff
result
end
Thus foo will return the contents of 'result' or false.
Notice also the *everything* returns a value, similar to functional
languages. Thus you can write:
def foo
if condition1
1
elsif condition2
2
else
3
end
End
Or:
def bar
case condition
when pattern1 then 1
when pattern2 then 3
when pattern3 then 4
end
end
See the end of the section "Invoking a method" in:
http://phrogz.net/ProgrammingRuby/language.html#invokingamethod
If you do not want to tell me because you dont think i
read the chm file, then please tell me where i can read how to do this
(simple examples would also be great)?
You'll usually find this list much friendlier than just RTFM, even
though reading the ProgrammingRuby book cover to cover is highly
recommended
See if you can rewrite your program better and
HTH,
Assaph