{newb} Help with ref

<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 :slight_smile:

See if you can rewrite your program better and

HTH,
Assaph

I don't think that's strictly true - there's no shadowing going on. As far
as I know, parameter 'data' already *is* a local variable, and you are just
reassigning it. For example:

  def foo(data)
    1.times do
      data = "x"
    end
    puts data
  end

  foo("hello") # prints "x"

If the second instance of 'data' were really a new local variable, it would
be local to the block and drop out of scope at the end of it, but it
doesn't.

However, the underlying point you make is clearly right: for
    data = data.upcase
then afterwards, data is pointing to a new object, which is a copy of the
original string but with characters uppercased. In fact, str.upcase is
implemented internally as str.dup.upcase!

[from string.c]
static VALUE
rb_str_upcase(str)
    VALUE str;
{
    str = rb_str_dup(str);
    rb_str_upcase_bang(str);
    return str;
}

Regards,

Brian.

ยทยทยท

On Mon, Sep 27, 2004 at 02:01:05PM +0900, Mehr, Assaph (Assaph) wrote:

Notice however in your programs that the line:
  data= data.upcase
In encrypt actually creates a local variable named data, overshadowing
the parameter data.