# Ruby loops and definitions help?

**URL:** <https://www.ruby-forum.com/t/ruby-loops-and-definitions-help/168055>\
**Category:** Ruby\
**Created:** [May 15, 2009, 11:21am UTC](https://www.ruby-forum.com/t/ruby-loops-and-definitions-help/168055 "2009-05-15T11:21:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 15, 2009, 11:21am UTC](https://www.ruby-forum.com/t/ruby-loops-and-definitions-help/168055/1 "2009-05-15T11:21:47Z")

</div>

Hello. I’m looking to construct a loop that defines an array of [apple,  
banana, cherry] like this:

@apple = apple  
@banana = banana  
@cherry = cherry

I’m thinking it’s something in this form:

[apple, banana, cherry].each do |attribute|  
@_attribute_ = attribute  
end

…but I have no idea what belongs in place of the _attribute_. Does  
anyone know?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 15, 2009, 11:51am UTC](https://www.ruby-forum.com/t/ruby-loops-and-definitions-help/168055/2 "2009-05-15T11:51:36Z")

</div>

Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> @_attribute_ = attribute  
> end
> 
> …but I have no idea what belongs in place of the _attribute_. Does  
> anyone know?

Yes. The objects in ruby do know if and when, how. You must ask them!  
For example, using ri(1).

irb(main):001:0\> (ri “Object”)

---------------------------------------------------------- Class: Object  
+Object+ is the parent class of all classes in Ruby. Its methods  
are therefore available to all objects unless explicitly  
overridden.

```
 +Object+ mixes in the +Kernel+ module, making the built-in kernel
 functions globally accessible. Although the instance methods of
 +Object+ are defined by the +Kernel+ module, we have chosen to
 document them here for clarity.

 In the descriptions of Object's methods, the parameter _symbol_
 refers to a symbol, which is either a quoted string or a +Symbol+
 (such as +:name+).

```

* * *

## Includes:

```
 Kernel(Array, Float, Integer, Pathname, String, URI, `, abort,
 at_exit, autoload, autoload?, binding, block_given?, callcc,
 caller, catch, chomp, chomp!, chop, chop!, eval, exec, exit, exit!,
 fail, fork, format, getc, gets, global_variables, gsub, gsub!,
 iterator?, lambda, load, local_variables, loop, method_missing,
 open, open, open_uri_original_open, p, pp, pretty_inspect, print,
 printf, proc, putc, puts, raise, rand, readline, readlines,
 require, scan, scanf, select, set_trace_func, sleep, split,
 sprintf, srand, sub, sub!, syscall, system, test, throw, trace_var,
 trap, untrace_var, warn, warn, y), PP::ObjectMixin(pretty_print,
 pretty_print_cycle, pretty_print_inspect,
 pretty_print_instance_variables)

```

## Constants:

```
 MatchingData: rb_cMatch
 ENV: envtbl
 ENV: envtbl
 TOPLEVEL_BINDING: rb_f_binding(ruby_top_self)
 STDIN: rb_stdin
 STDOUT: rb_stdout
 STDERR: rb_stderr
 ARGF: argf
 NIL: Qnil
 TRUE: Qtrue
 FALSE: Qfalse
 DATA: f
 ARGV: rb_argv
 RUBY_VERSION: v
 RUBY_RELEASE_DATE: d
 RUBY_PLATFORM: p
 RUBY_PATCHLEVEL: INT2FIX(RUBY_PATCHLEVEL)
 VERSION: v
 RELEASE_DATE: d
 PLATFORM: p
 IPsocket: rb_cIPSocket
 TCPsocket: rb_cTCPSocket
 SOCKSsocket: rb_cSOCKSSocket
 TCPserver: rb_cTCPServer
 UDPsocket: rb_cUDPSocket
 UNIXsocket: rb_cUNIXSocket
 UNIXserver: rb_cUNIXServer

```

## Class methods:

```
 new

```

## Instance methods:

```
 ==, ===, =~, __id__ , __send__ , class, clone, dclone, display, dup,
 enum_for, eql?, equal?, extend, freeze, frozen?, hash, id, inspect,
 instance_eval, instance_of?, instance_variable_get,
 instance_variable_get, instance_variable_set,
 instance_variable_set, instance_variables, is_a?, kind_of?, method,
 methods, nil?, object_id, private_methods, protected_methods,
 public_methods, remove_instance_variable, respond_to?, send,
 singleton_method_added, singleton_method_removed,
 singleton_method_undefined, singleton_methods, taint, tainted?,
 to_a, to_enum, to_s, to_yaml, to_yaml_properties, to_yaml_style,
 type, untaint

```

nil  
irb(main):002:0\> (ri “Object.instance\_variable\_set”)

## ------------------------------------------- Object#instance\_variable\_set obj.instance\_variable\_set(symbol, obj) =\> obj

```
 Sets the instance variable names by _symbol_ to _object_, thereby
 frustrating the efforts of the class's author to attempt to provide
 proper encapsulation. The variable did not have to exist prior to
 this call.

    class Fred
      def initialize(p1, p2)
        @a, @b = p1, p2
      end
    end
    fred = Fred.new('cat', 99)
    fred.instance_variable_set(:@a, 'dog') #=> "dog"
    fred.instance_variable_set(:@c, 'cat') #=> "cat"
    fred.inspect #=> "#<Fred:0x401b3da8 

```

@a="dog", @b=99, @c="cat"\>"

nil  
irb(main):003:0\> ( [“apple”,“banana”,“cherry”] . each { | attribute |  
(self . instance\_variable\_set( ((“@” + attribute) . to\_sym) ,  
attribute)) } )  
[“apple”, “banana”, “cherry”]  
irb(main):004:0\> @apple  
“apple”  
irb(main):005:0\> @banana  
“banana”  
irb(main):006:0\> @cherry  
“cherry”  
irb(main):007:0\> @pear  
nil  
irb(main):008:0\>

That said, an array like [apple, banana, cherry] will actually be an  
array such as [“value of apple”, 42, [“the”,:value,0,“cherry”]].  
If you want to build an array containing the name of the variable,  
then you have to make an array of symbols:  
[:apple,:banana,:cherry].

Then you will have to write:

irb(main):018:0\> (begin  
(apple = “value of apple”)  
(banana = 42)  
(cherry = [“the”,:value,0,“cherry”])  
( [:apple,:banana,:cherry] . each { | variable |  
(self . instance\_variable\_set( ((“@” + (variable . to\_s)) . to\_sym) ,  
(eval  
(variable . to\_s)))) })  
end)  
[:apple, :banana, :cherry]  
irb(main):025:0\> @apple  
“value of apple”  
irb(main):026:0\> @banana  
42  
irb(main):027:0\> @cherry  
[“the”, :value, 0, “cherry”]  
irb(main):028:0\>
