Accessing an instance variable in another method

Discussion in 'Ruby' started by Subashini Kumar, Sep 22, 2009.

  1. Hi,
    I am new to Rails.I tried developing a basic quiz application.Please
    help me out in my doubt.Thanks in advance

    <% CODE %>

    class QuizController < ApplicationController

    def index
    # i have retrieved the first five questions from the database for
    displaying the #question to user
    @quiz=Quiz.find:)all,:limit=>5)

    end

    def report

    end

    # this method checks the answer and gives score
    def checkanswer
    # here i want to access the @quiz object which is a set of questions i
    have #retreived from the database.Hot to access it?
    end

    end
    --
    Posted via http://www.ruby-forum.com/.
    Subashini Kumar, Sep 22, 2009
    #1
    1. Advertising

  2. Subashini Kumar

    Paul Smith Guest

    On Tue, Sep 22, 2009 at 1:28 PM, Subashini Kumar
    <> wrote:
    > Hi,
    > I am new to Rails.I tried developing a basic quiz application.Please
    > help me out in my doubt.Thanks in advance
    >
    > <% CODE %>
    >
    > class QuizController < ApplicationController
    >
    > =A0def index
    > # i have retrieved the first five questions from the database for
    > displaying the #question to user
    > =A0 =A0@quiz=3DQuiz.find:)all,:limit=3D>5)
    >
    > =A0end
    >
    > =A0def report
    >
    > =A0end
    >
    > # this method checks the answer and gives score
    > =A0def checkanswer
    > # here i want to access the @quiz object which is a set of questions i
    > have #retreived from the database.Hot to access it?


    Just use @quiz. That's the point of instance variables. However, you
    have to make sure that checkanswer is being called at some point after
    index has been called, and I'm no Rails aficionado.

    > =A0end

    --=20
    Paul Smith
    http://www.nomadicfun.co.uk

    Paul Smith, Sep 22, 2009
    #2
    1. Advertising

  3. Paul Smith wrote:
    > On Tue, Sep 22, 2009 at 1:28 PM, Subashini Kumar
    > <> wrote:
    >> displaying the #question to user
    >> # here i want to access the @quiz object which is a set of questions i
    >> have #retreived from the database.Hot to access it?

    >
    > Just use @quiz. That's the point of instance variables. However, you
    > have to make sure that checkanswer is being called at some point after
    > index has been called, and I'm no Rails aficionado.
    >
    >> �end

    > --
    > Paul Smith
    > http://www.nomadicfun.co.uk
    >
    >



    Hi,

    No that doesnt work..
    When i try to access @quiz.each {}

    i get an error like "there is no each method defined"
    --
    Posted via http://www.ruby-forum.com/.
    Subashini Kumar, Sep 22, 2009
    #3
  4. Hi --

    On Tue, 22 Sep 2009, Subashini Kumar wrote:

    > Hi,
    > I am new to Rails.I tried developing a basic quiz application.Please
    > help me out in my doubt.Thanks in advance
    >
    > <% CODE %>
    >
    > class QuizController < ApplicationController
    >
    > def index
    > # i have retrieved the first five questions from the database for
    > displaying the #question to user
    > @quiz=Quiz.find:)all,:limit=>5)
    >
    > end
    >
    > def report
    >
    > end
    >
    > # this method checks the answer and gives score
    > def checkanswer
    > # here i want to access the @quiz object which is a set of questions i
    > have #retreived from the database.Hot to access it?
    > end
    >
    > end


    There are two things you have to keep in mind here. The first is that
    every instance of a class has its own supply of instance variables:

    class Person
    def initialize(name)
    @name = name
    end

    def print_name
    puts @name
    end
    end

    a = Person.new("David")
    a.print_name # David
    b = Person.new("Joe")
    b.print_name # Joe

    So every time I create a new Person object, that object has a fresh
    "slate" of instance variables.

    The second thing you need to know is that in your Rails application,
    every request creates a new controller object. So the @quiz variable
    you create for the index request is not going to exist on the next
    request (i.e., when the person looking at the index clicks on a link
    and your application goes through another request/response cycle).
    Instance variables are "sticky", but only for the lifetime of the
    object.


    David

    --
    David A. Black, Director
    Ruby Power and Light, LLC (http://www.rubypal.com)
    Ruby/Rails training, consulting, mentoring, code review
    Book: The Well-Grounded Rubyist (http://www.manning.com/black2)
    David A. Black, Sep 22, 2009
    #4
  5. Subashini Kumar

    Ilan Berci Guest

    perhaps something like this..

    NOT TESTED.. at work.. just pseudo code to get you started and give you
    ideas.. not responsible for absolutely anything including but not
    limited to your computer blowing up from running the code below.

    class QuizController < ApplicationController

    def index
    @quizes=Quiz.find:)all,:limit=>5) # TODO shake this up with a random
    to get unique quizes each time
    end

    def report
    @report = "If you aren't using ruby, you failed!"
    end

    def checkanswer
    @quiz = Quiz.find(params[:quiz_id]) # get the quiz from the response
    form
    answers = @quiz.answers
    responses = params[:responses] # get the responses from the
    response form
    @correct_answer_count = answers.zip(responses).map(0) {|sum, a, r| a
    == r ? sum + 1 : sum}
    @percentage = (@correct_answer_count /
    @quiz_completed.questions.size) * 100
    end

    end

    Congrats on getting busy with rails, you are doing great and keep going!

    ilan
    --
    Posted via http://www.ruby-forum.com/.
    Ilan Berci, Sep 23, 2009
    #5
  6. Subashini Kumar

    Ilan Berci Guest

    Ilan Berci wrote:

    > @percentage = (@correct_answer_count /
    > @quiz_completed.questions.size) * 100
    > end
    >
    > ilan


    whoops.. typo from variable rename..
    @percentage = (@correct_answer_count / @quiz.questions.size) * 100

    ilan



    --
    Posted via http://www.ruby-forum.com/.
    Ilan Berci, Sep 23, 2009
    #6
    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. Kent Johnson
    Replies:
    4
    Views:
    391
    Terry Hancock
    Nov 13, 2004
  2. David Garamond
    Replies:
    5
    Views:
    216
    Ara.T.Howard
    Jun 8, 2004
  3. Raj Singh
    Replies:
    2
    Views:
    186
    Rick DeNatale
    May 29, 2008
  4. Greg Hauptmann
    Replies:
    9
    Views:
    233
    Loren Segal
    Jun 16, 2008
  5. Chuck Remes
    Replies:
    3
    Views:
    171
    hemant
    Aug 12, 2010
Loading...

Share This Page