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).
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).
In ruby there are no statements, just expressions. Therefore:
@a = @b = 0
The one caveat, though, is that the references are the same
which means that, for example, assigning “hello” this way
might lead to some confusing results.
Some alternatives include:
@a, @b = 0, 0
@a, @b = Array.new(2) {“Hello”}
Another popular idiom is to defer the initialisation to the
point of first use of the variable. For this the || operator
is often used:
do_something_with(@a || default_value)
do_something_else_with(@b ||= other_default)
The latter operator is shorthand for (@b = @b || default) which
will also assign the default to @b for future use.
In ruby there are no statements, just expressions. Therefore:
@a = @b = 0
The one caveat, though, is that the references are the same
which means that, for example, assigning “hello” this way
might lead to some confusing results…
Thank you, the first example will probably work in this case, but I can
experiment with your other examples.
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).
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