SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Location
    Denmark.
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    would like to use a string as an attribute (of a class)

    I would like to use a string as an attribute (of a class)

    example:
    o=Someclass.new
    attr='name'

    then if executing
    puts o.attr

    gives the error: ...attr is not a method...

    any help appreciated

    Erik

  2. #2
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your someClass could look like this:
    Code:
    class Someclass
      attr_accessor :attr
    
      def initialize(attr)
        @attr = attr
      end
    end
    The attr_accessor call creates a getter and a setter method for you, so you can access the attribute @attr, which is private.

    If your only OO language is JavaScript, then you will need to spend a few hours learning how Ruby's classes and methods work. The may look similar if you squint and look at them sideways, but they are conceptually very different.

  3. #3
    SitePoint Member
    Join Date
    May 2007
    Location
    Denmark.
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    more explanation here

    Quote Originally Posted by erik8085 View Post
    example:
    o=Someclass.new
    attr='name'

    then if executing
    puts o.attr

    gives the error: ...attr is not a method...
    Sorry, more explanation here:
    attr='name'
    so when o.attr evaluates, I play that it is o.name, and name is an attribute of Someclass.
    Rephrase of question: is there a way to use a stringValue instead of a attributeName (typeconvert or other thing)

  4. #4
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I got it now, sorry for the misunderstanding. Yes, in fact you can do that too, and in one line. Just call the send method:
    Code:
    puts o.send(attr)
    You can include arguments after the method name, too:
    Code:
    o.send('name=','Bob')

  5. #5
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or, in this case:
    Code:
    o.send(attr + '=','Bob')
    # or
    o.send("#{attr}=", 'Bob')
    If you give someone a program,
    you will frustrate them for a day;
    if you teach them how to program,
    you will frustrate them for a lifetime.

  6. #6
    SitePoint Member
    Join Date
    May 2007
    Location
    Denmark.
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ended up with:
    o.send(ATTRS[index] + '=',item)
    ------------------------------------

    ATTRS is an array of instance attributes which exist in Someclass.
    Value of item comes from a text file with lines of values, like:

    "ssa","asa","sdsd","sdsd","sdsd"
    "svsa","asbcxvba","sxcvbdsd","svbndsd","scvndsd"
    "snsa","acsa","sdnmsd","sdnmsd","snbmdsd"
    .
    .
    .
    Thanks for the help
    Erik


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
  •