[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

hyphens in variable names

Paul Hepworth

7/7/2006 3:52:00 PM

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-....

8 Answers

Robert Klemme

7/7/2006 4:12:00 PM

0

2006/7/7, Paul Hepworth <paul@hepworthinc.com>:
> 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-....
>
>


--
Have a look: http://www.flickr.com/photos/fu...

Jacob Fugal

7/7/2006 4:14:00 PM

0

On 7/7/06, Paul Hepworth <paul@hepworthinc.com> 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

7/7/2006 4:19:00 PM

0

On 7/7/06, Robert Klemme <shortcutter@googlemail.com> 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

Paul Hepworth

7/7/2006 4:24:00 PM

0

Robert Klemme wrote:
> 2006/7/7, Paul Hepworth <paul@hepworthinc.com>:
>> 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-....

Paul Hepworth

7/7/2006 4:27:00 PM

0

Jacob Fugal wrote:
> On 7/7/06, Robert Klemme <shortcutter@googlemail.com> 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-....

Robert Klemme

7/7/2006 4:41:00 PM

0

2006/7/7, Paul Hepworth <paul@hepworthinc.com>:
> > 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/fu...

Paul Hepworth

7/7/2006 4:48:00 PM

0

You guys are great. Thanks so much!

--
Posted via http://www.ruby-....

Carrie

6/23/2009 5:25:00 PM

0


"JRad" <jradgosky@yahoo.com> wrote in message
news:2822755d-280f-47d3-a65c-5bb178f9cf71@s12g2000yqi.googlegroups.com...
On Jun 23, 8:37 am, "Carrie" <starchild1...@charter.net> wrote:
> "JRad" <jradgo...@yahoo.com> wrote in message
>
> news:bd8520e2-c57e-442d-b8ef-b53a20627ed4@y9g2000yqg.googlegroups.com...
> On Jun 22, 11:03 pm, Carrie <starchild1...@yahoo.com> wrote:
>
> > It's late, and I don't have time to find the exact thread and post.
> > I think it was in one of the long Ted/JohnR ones...
>
> > Made me think of this:
>
> >http://photos.bravenet.com/869/324/22/0F93...
>
> ok, i confess ... the fact is, it was actually a typo, well, sort of.
> I did write it but then thought it was inappropriate. And I had
> intended to delete it but pushed that send button too quickly. So, I
> take responsibility but it was a mistake. Sorry.
>
> I thought it was funny
> I once got a card with that picture on it, something like "having a bad
> day?"
> And scanned it. I used it on one of my discussion boards one time with
> "whatever" under it, too.
> Someone said it reminded him of his mother in law in the morning.
> As to you posting it, what happened to "there are no accidents"?
>
> jr

the course does mention "mistakes" so we have to be able to account
for them, correct ? If it was not an accident, then it certainly was
a mistake.

I think it was (and is) funny.
Seeing something as a "mistake" and then trying to correct it.... what I
think of it saying as the simple line:
"Acknowledge but you have been mistaken and all effects of your mistake
disappear"
No big deal.
You don't think PEACE PUSSY (picture) is funny and/or cute?



JR