[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Using a '-' in a Key name in a Hash

Andrew Knott

7/28/2006 3:26:00 PM

Hi,

This a ruby question but it comes up in the context of Rails, hope that
is OK.

I am trying to to consume a web service that has a structure element
called 'remote-ip'. (as below)

result = server.call("IsValid",
{:username=>"me",:password=>"pass",:remote-ip=>request.remote_ip},@formattednumber)


This ofcourse doesn't work right now because I can't have a '-' in a key
name? Is there any way around this or am I screwed?

thanks so much for any help.

Andrew

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

11 Answers

Sean O'Halpin

7/28/2006 3:32:00 PM

0

On 7/28/06, Andrew Knott <aknott@mac.com> wrote:
> Hi,
>
> This a ruby question but it comes up in the context of Rails, hope that
> is OK.
>
> I am trying to to consume a web service that has a structure element
> called 'remote-ip'. (as below)
>
> result = server.call("IsValid",
> {:username=>"me",:password=>"pass",:remote-ip=>request.remote_ip},@formattednumber)
>
>
> This ofcourse doesn't work right now because I can't have a '-' in a key
> name? Is there any way around this or am I screwed?
>
> thanks so much for any help.
>
> Andrew
>
To include non-identifier characters in a symbol, use the quoted form,
i.e. :"remote-ip" => request.remote_ip

Regards,
Sean

Andrew Knott

7/28/2006 3:41:00 PM

0

Thanks a lot!

Sean O'halpin wrote:
> On 7/28/06, Andrew Knott <aknott@mac.com> wrote:
>>
>>
>> This ofcourse doesn't work right now because I can't have a '-' in a key
>> name? Is there any way around this or am I screwed?
>>
>> thanks so much for any help.
>>
>> Andrew
>>
> To include non-identifier characters in a symbol, use the quoted form,
> i.e. :"remote-ip" => request.remote_ip
>
> Regards,
> Sean


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

Andrew Knott

7/28/2006 4:09:00 PM

0

Ok, that's worked perfectly for the call... Unfortunately on the server
side I have a member of the same name in an Action Webservice Struct and
it's not liking the

member :"remote-ip", :string

at all...

SyntaxError ((eval):1:in `member': compile error
(eval):1: parse error, unexpected '-', expecting '\n' or ';'
def remote-ip; @remote-ip; end

I guess this might be bug in rails...

thanks for all the help...

Andrew

Andrew Knott wrote:
> Thanks a lot!
>
> Sean O'halpin wrote:
>> On 7/28/06, Andrew Knott <aknott@mac.com> wrote:
>>>
>>>
>>> This ofcourse doesn't work right now because I can't have a '-' in a key
>>> name? Is there any way around this or am I screwed?
>>>
>>> thanks so much for any help.
>>>
>>> Andrew
>>>
>> To include non-identifier characters in a symbol, use the quoted form,
>> i.e. :"remote-ip" => request.remote_ip
>>
>> Regards,
>> Sean


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

Daniel Schierbeck

7/28/2006 6:36:00 PM

0

Andrew Knott wrote:
> Ok, that's worked perfectly for the call... Unfortunately on the server
> side I have a member of the same name in an Action Webservice Struct and
> it's not liking the
>
> member :"remote-ip", :string
>
> at all...
>
> SyntaxError ((eval):1:in `member': compile error
> (eval):1: parse error, unexpected '-', expecting '\n' or ';'
> def remote-ip; @remote-ip; end
>
> I guess this might be bug in rails...

No; "remote-ip" is here the name of a method and an instance variable,
but neither can contain `-'.


Daniel

Ara.T.Howard

7/28/2006 6:51:00 PM

0

Andrew Knott

7/28/2006 7:09:00 PM

0

I would agree...

unknown wrote:
>>> I guess this might be bug in rails...
>>
>> No; "remote-ip" is here the name of a method and an instance variable, but
>> neither can contain `-'.
>
> but i think rails is autogenerating this method - it's a major design
> flaw if
> so.
>
> -a


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

Matt Todd

7/28/2006 7:21:00 PM

0

Forgive my unfamiliarity with Rails, but in what context is this call
to member? I'm trying to figure out where to look in the source of
Rails for a possible solution (or to write a patch), but I'm just not
connecting it. Is it in ActiveResource?

M.T.

Daniel Schierbeck

7/28/2006 9:13:00 PM

0

ara.t.howard@noaa.gov wrote:
> On Sat, 29 Jul 2006, Daniel Schierbeck wrote:
>
>> Andrew Knott wrote:
>>> Ok, that's worked perfectly for the call... Unfortunately on the
>>> server side I have a member of the same name in an Action Webservice
>>> Struct and it's not liking the
>>>
>>> member :"remote-ip", :string
>>>
>>> at all...
>>>
>>> SyntaxError ((eval):1:in `member': compile error
>>> (eval):1: parse error, unexpected '-', expecting '\n' or ';'
>>> def remote-ip; @remote-ip; end
>>>
>>> I guess this might be bug in rails...
>>
>> No; "remote-ip" is here the name of a method and an instance variable,
>> but neither can contain `-'.
>
> but i think rails is autogenerating this method - it's a major design
> flaw if
> so.

Yeah, I guess it would be smarter to replace the hyphen with an underscore.

Andrew, note that methods can actually have such names, they just can't
be called or defined the normal way;

class Foo
define_method("foo-bar"){"FUBAR!!!!1!one"}
end

Foo.new.send("foo-bar") #=> "FUBAR!!!!1!one"


Cheers,
Daniel

Matt Todd

7/28/2006 10:29:00 PM

0

@Daniel: That's very cool. So then the change for getting Andrew
Knott's stuff working could be minimal!

So, again, what's the context of the call? From what library?
ActiveResource or something else?

M.T.

Andrew Knott

7/29/2006 7:23:00 AM

0

In ActionWebService::Structs you define members using

member :name, :string

in my case the member needed to be

member :"remote-ip", :string

Which ofcourse doesn't work, the syntax error comes as follows (line 53
in struct.rb)

SyntaxError ((eval):1:in `member': compile error
(eval):1: parse error, unexpected '-', expecting '\n' or ';'
def remote-ip; @remote-ip; end
^
(eval):1: parse error, unexpected kEND, expecting $):
/usr/local/lib/ruby/gems/1.8/gems/actionwebservice-1.1.4/lib/action_web_service/struct.rb:53:in
`member'
/app/models/msisdn_call.rb:4



Matt Todd wrote:
> @Daniel: That's very cool. So then the change for getting Andrew
> Knott's stuff working could be minimal!
>
> So, again, what's the context of the call? From what library?
> ActiveResource or something else?
>
> M.T.


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