Having problems with my instance variable

Discussion in 'Ruby' started by macaco, Feb 5, 2008.

  1. macaco

    macaco Guest

    [Note: parts of this message were removed to make it a legal post.]

    I'm sorry if this question seem stupid, but couldn't find it in the books.

    I have this code in the models directory:
    class Figure
    attr_reader :xpos, :ypos

    def initialize
    @xpos = 0
    @ypos = 0
    @other = 0
    @another = 0
    end

    def some
    call_other(@xpos,@ypos)
    end

    def more
    call_other(@other,@another)
    end

    def call_other(var1, var2)
    max = 9
    var1= rand(max)
    var2=rand(max)
    end
    end


    And have this on my controller:

    def figure
    fig = Figure.new
    fig.some
    render :text => "<h1>new values</h1>"+fig.xpos.to_s+" "+fig.ypos.to_s
    end

    Problem is, it always print 0 0... as if the random wouldn't work, but I
    have test the random outside this method (calling it in the first method,
    not the second one), but the second method is one I need cos I use it too
    many times and with different instance variables

    Any help?
    macaco, Feb 5, 2008
    #1
    1. Advertising

  2. macaco

    Day Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Feb 4, 2008 8:14 PM, macaco <> wrote:

    > def some
    > call_other(@xpos,@ypos)
    > end
    >
    > def more
    > call_other(@other,@another)
    > end
    >
    > def call_other(var1, var2)
    > max = 9
    > var1= rand(max)
    > var2=rand(max)
    > end
    > end
    >


    It's not clear to me what you're trying to do here. You want a method that
    will set arbitrary variables to random numbers? That's a wild guess. What
    you're actually doing when you call call_other is passing it the values in
    @xpos and @ypos (which is 0 and 0 initially). Then, in call_other, you're
    taking those 0s in with var1 and var2. Then you're over-writing var1 and
    var2 with random numbers (0s are lost, as far as this method is concerned)
    and then... you exit. You're leaving your @vars untouched because you're
    passing the values, not the variables themselves.

    If "max" is always going to be the same, I'd make it a constant at the class
    level and then just have some look like this:

    def some
    @xpos = rand MAX
    @ypos = rand MAX
    end

    If max won't be the same all the time, make a method that figures it out and
    returns a random number based on it... An example will make more sense,
    maybe.

    def some
    @xpos = generate_random
    @ypos = generate_random
    end

    def generate_random
    max = 9 # or whatever determines your max value
    rand(max)
    end

    I hope that's clear. Scope can be confusing sometimes. If I am too rambly (I
    hear that a lot), maybe someone with more experience will be more clear,
    too.


    Ben
    Day, Feb 5, 2008
    #2
    1. Advertising

  3. macaco

    Andrew Stone Guest

    [Note: parts of this message were removed to make it a legal post.]

    > def some
    > call_other(@xpos,@ypos)
    > end
    >
    > def more
    > call_other(@other,@another)
    > end
    >


    This worked:
    class Test
    attr_reader :xpos

    def initialize
    @xpos = 0
    end

    def some
    p "before call: #{@xpos}"
    #notice the colon
    c :mad:xpos
    p "after call: #{@xpos}"
    end

    def call_other(var)
    max = 9
    self.instance_variable_set(var, rand(max))
    end
    end

    begin
    t = Test.new
    t.some
    end

    wyz@local ~ $ ruby test.rb
    "before call: 0"
    "after call: 7"

    wyz@local ~ $ ruby test.rb
    "before call: 0"
    "after call: 2"

    Warning: I'm not feeling well and the NyQuil is kicking in... :/

    --
    Andrew Stone
    Andrew Stone, Feb 5, 2008
    #3
  4. macaco

    macaco Guest

    [Note: parts of this message were removed to make it a legal post.]

    Thanks Day, but this is just a simplification of what I need

    @Andrew Stone, thanks instance_variable_set makes it's work, but what if i
    need the data inside too... for example

    def call_other(var1,var2)
    max = 8
    if(var1 == 0)
    var1 = rand max
    else
    var1 += max
    end
    var2 = rand max
    end


    BTW, can you explain the line of code "c :mad:xpos", didn't get it

    Thanks again

    On Feb 4, 2008 11:03 PM, Andrew Stone <> wrote:

    > > def some
    > > call_other(@xpos,@ypos)
    > > end
    > >
    > > def more
    > > call_other(@other,@another)
    > > end
    > >

    >
    > This worked:
    > class Test
    > attr_reader :xpos
    >
    > def initialize
    > @xpos = 0
    > end
    >
    > def some
    > p "before call: #{@xpos}"
    > #notice the colon
    > c :mad:xpos
    > p "after call: #{@xpos}"
    > end
    >
    > def call_other(var)
    > max = 9
    > self.instance_variable_set(var, rand(max))
    > end
    > end
    >
    > begin
    > t = Test.new
    > t.some
    > end
    >
    > wyz@local ~ $ ruby test.rb
    > "before call: 0"
    > "after call: 7"
    >
    > wyz@local ~ $ ruby test.rb
    > "before call: 0"
    > "after call: 2"
    >
    > Warning: I'm not feeling well and the NyQuil is kicking in... :/
    >
    > --
    > Andrew Stone
    >
    macaco, Feb 5, 2008
    #4
  5. macaco

    Day Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Feb 5, 2008 11:37 AM, macaco <> wrote:

    > @Andrew Stone, thanks instance_variable_set makes it's work, but what if i
    > need the data inside too...
    >


    I think instance_variable_get? Check out http://www.ruby-doc.org/core/ and
    look in the right column for methods that start with instance_v. Or do a
    find on _variable and see what turns up.


    Ben
    Day, Feb 5, 2008
    #5
    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. Paul Carey

    Instance Variable vs Local Variable

    Paul Carey, Dec 3, 2003, in forum: Java
    Replies:
    3
    Views:
    782
    Chris Uppal
    Dec 3, 2003
  2. chris brat
    Replies:
    1
    Views:
    612
    chris brat
    May 10, 2006
  3. Gerry Sutton
    Replies:
    1
    Views:
    523
    Peter Otten
    Apr 16, 2005
  4. David Garamond
    Replies:
    5
    Views:
    212
    Ara.T.Howard
    Jun 8, 2004
  5. Leon Bogaert
    Replies:
    19
    Views:
    311
    Robert Klemme
    Mar 23, 2008
Loading...

Share This Page