From: Markus Date: 2004-09-27T14:39:11+09:00 Subject: Re: {newb} Help with ref I will try tell you what I think want you want to know, rather than what you asked. If this leaves you with questions, please post again and I will try again (maybe tomorrow, since it is getting late here). First, the easy question. You can return a value from a function by writing: return x or, if you let control "fall out the bottom" the last value in the expression will be returned. For an example of both forms: def factorial(x) return 1 if x <= 1 x*factorial(x-1) end The more complex problem you are having is not that references are to "memory locations" but that they are to objects. It may seem that these are the same thing (since in both cases you can wind up with a situation like this: s = "test" s2 = s s[0,1] = "b" -- and now _both_ strings have changed) but really they are very different. In languages like C it is easy to mess with the storage of something, and even corrupt it, but it is often hard to make a new thing. So you wind up often modifying things in place. In ruby, it is the other way around: easy to make new things but harder to modify them. For example, a somewhat modified version of your decrypt routine, which is somewhat more in the sprit of ruby might look like: WARNING: CODE TYPED DIRECTLY INTO MAIL MESSAGE (UNTESTED) def decrypt(data,shift = 27) if shift < 27 then # if not 27 they know key print data.upcase.split(//).collect { |c| i = c[0] - shift i += 26 if i < 65 i.chr }.join.downcase," key=",shift,"\n" #plain text lower case else for b in 0..25 # loop ever case decrypt(data,b) end end end The main change is that we upcase the string, split it into characters, muck with the character's values, join them together, and downcase them all in one go. For an even more rubyesque way to do this, consider something like this: WARNING: CODE TYPED DIRECTLY INTO MAIL MESSAGE (UNTESTED) $letters = ('A'.."Z').to_a.join def decrypt(data,shift = 27) if shift < 27 then # if not 27 they know key shifted_letters = $letters[shift..-1]+$letters[0..shift-1] print data.upcase.tr($letters,shifted_letters).downcase," key=",shift,"\n" #plain text lower case else for b in 0..25 # loop ever case decrypt(data,b) end end end Does this help? -- Markus On Sun, 2004-09-26 at 20:56, STEPHEN BECKER I V wrote: > ########################### code start > def decrypt(data,shift = 27) > if shift<27 then # if not 27 they know key > datanew=String.new(data.upcase) > a = datanew.size-1 # control loop > while a>-1 > if datanew[a] - shift<65 then #check if it is out of bound of > datanew[a]=datanew[a]-shift+26#works in my head > else datanew[a]=datanew[a]-shift > end > a-=1 > end > print datanew.downcase," key=",shift,"\n" #plain text lower case > else > for b in 0..25 # loop ever case > decrypt(data,b) > end > end > end > > def encrypt(data,shift = rand(26)) > > a = data.size-1 > while a>-1 > if data[a]+shift>122 then > data[a]=data[a]+shift-26 > else data[a]=data[a]+shift > end > a-=1 > end > data= data.upcase > print data," key=",shift,"\n" > end > > > it=String.new("abcdefghij") > encrypt(it) > decrypt(it) > ############################code end > it is the wonderful ceaser shift, you add or subtract a key to the char. > 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? also how do you return a value in a > function(def)? 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)? > Thank you for your time. > Stephen Becker