SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Subclass instance variable question

    I want to be able to do the following. How would I go about it? (without class variables)

    Code Ruby:
    class Message
     
      def setMessage(message)
        message
      end
    end
     
    class NewMessage < Message
     
      attr_accessor :test
     
      def intialize
        @test = setMessage("Hello World")
      end
     
      def output
        @test
      end
     
    end
     
    puts NewMessage.new.output -> "Hello World"
    Last edited by The New Guy; Aug 14, 2007 at 05:49.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  2. #2
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure what you mean, what you have works.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  3. #3
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    *sigh* My bad. I fixed it to show the problem I am having. I think I am more interested in why it doesn't work.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why it doesn't work.
    It doesn't work? I see no problem except unconventional names (setMessage vs set_message), and wrong names (setMessage doesn't *set* the message).

    What you are using are instance variables, not class variables.

    BTW, attr_accessor already defines test() and test=() methods:

    Code:
    puts NewMessage.new.test -> "Hello World"
    You can, of course, cook your own objects with instance variables:

    Code ruby:
    message = lambda do |var|
       lambda do |action|
         case action # dispatch
         when :set: lambda{|x| var = x} # setter method
         when :get: lambda{var} # getter
         end
       end
    end.call(nil) # initial value is nil
     
    message[:set]["Hello, world."]
    message[:get][] #=> "Hello, world."
     
    # same as:
     
    message.call(:set).call("Hello, world.")
    message.call(:get).call()
    Last edited by Fenrir2; Aug 14, 2007 at 09:51.

  5. #5
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    It doesn't work?
    No, it puts "nil". Also, this is just junk code to express the problem please ignore the aesthetics of the code.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  6. #6
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Typo: "intialize".

  7. #7
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    Typo: "intialize".
    Now, I feel like a even bigger fool. Sorry, to for wasting your time...

    The problem I was originally looking for an answer for, before I mucked this thread up, was found here:

    http://railstips.org/2006/11/18/clas...iables-in-ruby

    Thanks for your time Fenrir
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •