Ruby loops and definitions help?

Discussion in 'Ruby' started by Bob Sanders, May 15, 2009.

  1. Bob Sanders

    Bob Sanders Guest

    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?
    --
    Posted via http://www.ruby-forum.com/.
    Bob Sanders, May 15, 2009
    #1
    1. Advertising

  2. Bob Sanders <> writes:

    > 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?


    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>


    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, May 15, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    3
    Views:
    403
    Jason
    Aug 21, 2003
  2. Panda2
    Replies:
    2
    Views:
    648
  3. Steve Midgley
    Replies:
    0
    Views:
    72
    Steve Midgley
    Sep 28, 2006
  4. kunjaan
    Replies:
    8
    Views:
    111
    Charles Oliver Nutter
    May 28, 2009
  5. Me
    Replies:
    2
    Views:
    237
Loading...

Share This Page