Re: Beginner's question: assigning same value to many variables



-----Original Message-----
From: Morton Goldberg [mailto:m_goldberg@xxxxxxxxxxxxx]
Sent: Friday, August 11, 2006 9:21 PM
To: ruby-talk ML
Subject: Re: Beginner's question: assigning same value to
many variables

I presume you want each variable initialized to a _different_ empty
string. If so, I can't think of an easy way to do it using local
variables. That may just go to show my lack of ruby smarts. However,
it's not too hard to initialize a group of instance variables to
different empty strings in one line of code. Consider:

#! /usr/bin/ruby -w

class Foo
def initialize
%w[@a @b @c @d].each {|v| instance_variable_set(v, '')}
end
end

foo = Foo.new
p foo #=> #<Foo:0x2556c @c="", @b="", @a="", @d="">

And the following will work for global variables.

%w[$a, $b, $c, $d].each {|v| eval("%s=String.new" % v)}
p [$a, $b, $c, $d] #=> ["", "", "", ""]

But for local variables, I don't know.

How about this:

irb(main):001:0> a, b, c = Array.new(3) { '' }
=> ["", "", ""]
irb(main):002:0> a
=> ""
irb(main):003:0> b
=> ""
irb(main):004:0> c
=> ""
irb(main):005:0> a.object_id
=> 203740
irb(main):006:0> b.object_id
=> 203730
irb(main):007:0> c.object_id
=> 203720
irb(main):008:0>

It will work for global and instance variables as well.

Best,
Gennady.


Regards, Morton

On Aug 11, 2006, at 7:12 PM, Alex Khere wrote:

I'm just starting out in programming, using Ruby to learn.

I'm trying to write a short program that will use a handful of
variables, and I want to be sure that all of the variables
start with
the value '' (strings of zero length).

Is there a way to list all of the variables on a single line and set
them to the same value? I tried using commas to seperate, but that
didn't work.

I also tried creating an array with all of the variable
names and then
using "arrayname.each do |name|" to cycle through the
assignment, but
since the varibles hadn't been defined, I got an "undefined local
variable or method" error (at least, I think that's why I got an
error).




.



Relevant Pages

  • Re: CString
    ... My customers don't give a hoot in hell who forgot to initialize a variable --- if the software comes from me, ... Empty(), or set the string explicitly, always, before assuming anything. ... IMO, if you dont need a function call, dont call it. ...
    (microsoft.public.vc.mfc)
  • Re: Read line from TXT file
    ... empty line is read, so it is the wrong approach. ... variable empty because the variable should not participate in the loop test. ... initialize it at all ... because all CStrings are constructed with an empty string. ...
    (microsoft.public.vc.mfc)
  • Re: Initialisation Code - Strings with Null Values
    ... Are you sure that you are setting the string vDataMember field to an empty ... initialize it to an empty string contrary to your posted message. ...
    (microsoft.public.dotnet.framework.windowsforms.designtime)
  • Re: Record type flushing
    ... If you try to assign a new value to a string field in that ... reference, so the compiler's code tries to release that reference before ... The Initialize procedure can be used to solve that problem. ... If you're using the shell's memory manager, ...
    (alt.comp.lang.borland-delphi)
  • Re: Initializing static readonly methods
    ... public class TestClass ... //So assigning a value to readonly is perfectly fine. ... //readonly values are has to be initialized in constructor. ... There is no other place to initialize them. ...
    (microsoft.public.dotnet.languages.csharp)