hyphens in variable names

Discussion in 'Ruby' started by Paul Hepworth, Jul 7, 2006.

  1. Hello,

    I am new to Ruby and have an issue with a xmlsimple object resulting
    from a 3rd party webservice.

    The xml has nodes that have hyphens (-) in the names. When I try to
    access various hashes using the object variables I get errors telling
    that the variable doesn't exists. I realize that the ruby syntax doesnot
    like hyphens in variable names, how do I get around this?

    Example XML:

    <object>
    <tree-lists>
    <tree-list>
    ...
    </tree-list>
    ...
    </tree-lists>
    </object>

    Example Ruby :

    for tlist in object.tree-lists
    ...
    end

    Error would be:

    NoMethodError in TreeController#importproject
    undefined method `tree' for #<TreeLib::Record:0x39171e8>


    Thanks in advance.
    Paul

    --
    Posted via http://www.ruby-forum.com/.
    Paul Hepworth, Jul 7, 2006
    #1
    1. Advertising

  2. 2006/7/7, Paul Hepworth <>:
    > Hello,
    >
    > I am new to Ruby and have an issue with a xmlsimple object resulting
    > from a 3rd party webservice.
    >
    > The xml has nodes that have hyphens (-) in the names. When I try to
    > access various hashes using the object variables I get errors telling
    > that the variable doesn't exists. I realize that the ruby syntax doesnot
    > like hyphens in variable names, how do I get around this?
    >
    > Example XML:
    >
    > <object>
    > <tree-lists>
    > <tree-list>
    > ...
    > </tree-list>
    > ...
    > </tree-lists>
    > </object>
    >
    > Example Ruby :
    >
    > for tlist in object.tree-lists
    > ...
    > end
    >
    > Error would be:
    >
    > NoMethodError in TreeController#importproject
    > undefined method `tree' for #<TreeLib::Record:0x39171e8>


    Either use method send or use another tool to work with XML or use
    another way to access objects (if this lib provides one, maybe
    object["tee-list"] or object[:tree-list]).

    Kind regards

    robert
    >
    > Thanks in advance.
    > Paul
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >



    --
    Have a look: http://www.flickr.com/photos/fussel-foto/
    Robert Klemme, Jul 7, 2006
    #2
    1. Advertising

  3. Paul Hepworth

    Jacob Fugal Guest

    On 7/7/06, Paul Hepworth <> wrote:
    > I am new to Ruby and have an issue with a xmlsimple object resulting
    > from a 3rd party webservice.
    >
    > The xml has nodes that have hyphens (-) in the names...
    >
    > Example XML:
    >
    > <object>
    > <tree-lists>
    > <tree-list>
    > ...
    > </tree-list>
    > ...
    > </tree-lists>
    > </object>
    >
    > Example Ruby :
    >
    > for tlist in object.tree-lists
    > ...
    > end


    I'm not certain how xmlsimple works, but I assume it's dynamically
    generating methods (or using method_missing) to map methods as
    pseudo-properties onto the XML elements. If so, this *might* work:

    for tlist in object.send:)'tree-lists')
    ...
    end

    Ruby doesn't like you having methods with hyphens in the name
    *syntactically*, but semantically, there's nothing wrong with it. You
    can create methods with hyphenated names using define_method, just not
    def. And you can call methods with hyphenated names using send, just
    not the standard dot syntax. It is of course discouraged, being highly
    ugly, but it is *possible*. :)

    Jacob Fugal
    Jacob Fugal, Jul 7, 2006
    #3
  4. Paul Hepworth

    Jacob Fugal Guest

    On 7/7/06, Robert Klemme <> wrote:
    > Either use method send or use another tool to work with XML or use
    > another way to access objects (if this lib provides one, maybe
    > object["tee-list"] or object[:tree-list]).


    I agree with Robert that if the lib provides hash-style access, that's
    probably a cleaner way to go. Note, though, that if the hash needs a
    symbol rather than a string, it will need to use quotes also, as I did
    in my other post:

    object[:'tree-list']

    Omitting the quotes would give a syntax error:

    $ irb
    >> hash = {}

    => {}
    >> hash[:tree-list]

    NameError: undefined local variable or method `list' for main:Object

    Jacob Fugal
    Jacob Fugal, Jul 7, 2006
    #4
  5. Robert Klemme wrote:
    > 2006/7/7, Paul Hepworth <>:
    >> Example XML:
    >> Example Ruby :
    >>
    >> for tlist in object.tree-lists
    >> ...
    >> end
    >>
    >> Error would be:
    >>
    >> NoMethodError in TreeController#importproject
    >> undefined method `tree' for #<TreeLib::Record:0x39171e8>

    >
    > Either use method send or use another tool to work with XML


    Are you referring to something like:
    object.send('tree-list')

    I did a quick search, but have not tried it yet.

    > or use
    > another way to access objects (if this lib provides one, maybe
    > object["tee-list"] or object[:tree-list]).


    The tree-lists and tree-list are both hashes, but for some reason I
    still get exceptions. I will keep playing with it. Surely I am not the
    only one that has run into this problem.

    Thanks for your help!

    >
    > Kind regards
    >
    > robert



    --
    Posted via http://www.ruby-forum.com/.
    Paul Hepworth, Jul 7, 2006
    #5
  6. Jacob Fugal wrote:
    > On 7/7/06, Robert Klemme <> wrote:
    >> Either use method send or use another tool to work with XML or use
    >> another way to access objects (if this lib provides one, maybe
    >> object["tee-list"] or object[:tree-list]).

    >
    > I agree with Robert that if the lib provides hash-style access, that's
    > probably a cleaner way to go. Note, though, that if the hash needs a
    > symbol rather than a string, it will need to use quotes also, as I did
    > in my other post:
    >
    > object[:'tree-list']


    Now that makes sense. I never tried it like this. Wow, what an insight.
    :)

    >
    > Omitting the quotes would give a syntax error:
    >
    > $ irb
    > >> hash = {}

    > => {}
    > >> hash[:tree-list]

    > NameError: undefined local variable or method `list' for main:Object


    That is the error that I saw all too often.

    >
    > Jacob Fugal



    --
    Posted via http://www.ruby-forum.com/.
    Paul Hepworth, Jul 7, 2006
    #6
  7. 2006/7/7, Paul Hepworth <>:
    > > Omitting the quotes would give a syntax error:
    > >
    > > $ irb
    > > >> hash = {}

    > > => {}
    > > >> hash[:tree-list]

    > > NameError: undefined local variable or method `list' for main:Object

    >
    > That is the error that I saw all too often.


    Yeah, that's caused by Ruby parsing ":foo" "-" "bar". Sorry, I should
    have used the proper syntax, Jacob is right of course, you need
    :"foo-bar" or :'foo-bar'. So the list of options now looks like this

    object.send "foo-bar"
    object.send :"foo-bar"
    object["foo-bar"]
    object[:"foo-bar"]

    Plus same methods with double quotes replaced by single quotes.

    robert

    --
    Have a look: http://www.flickr.com/photos/fussel-foto/
    Robert Klemme, Jul 7, 2006
    #7
  8. Paul Hepworth, Jul 7, 2006
    #8
    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. ibiza
    Replies:
    5
    Views:
    1,156
    S. Justin Gengo [MCP]
    Feb 9, 2006
  2. spaghetti

    Re: "Soft" hyphens

    spaghetti, Jun 24, 2003, in forum: HTML
    Replies:
    0
    Views:
    913
    spaghetti
    Jun 24, 2003
  3. Jukka K. Korpela

    Re: "Soft" hyphens

    Jukka K. Korpela, Jun 24, 2003, in forum: HTML
    Replies:
    0
    Views:
    921
    Jukka K. Korpela
    Jun 24, 2003
  4. Augustus

    Re: "Soft" hyphens

    Augustus, Jun 24, 2003, in forum: HTML
    Replies:
    4
    Views:
    884
    Anonymous Joe
    Jun 25, 2003
  5. James Harris
    Replies:
    46
    Views:
    1,004
    Ronald Landheer-Cieslak
    Jul 23, 2004
Loading...

Share This Page