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

A

Andrew Knott

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
 
S

Sean O'Halpin

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
 
A

Andrew Knott

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
 
D

Daniel Schierbeck

Andrew said:
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
 
A

ara.t.howard

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
 
M

Matt Todd

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

Daniel Schierbeck

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
 
M

Matt Todd

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

Andrew Knott

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
 
M

Matt Todd

54,55c54,55
< def #{name}; @#{name}; end
< def #{name}=(value); @#{name} = value; end
---
define_method("#{name}") { @#{name} }
define_method("#{name}=(value)") { @#{name} = value }

Give that diff file a try, just run:

patch $APP/vendor/rails/actionwebservice/lib/actionwebservice
/path/to/patch/file.diff

That should move it from calling plain-old class def, and use this
better method. Also, just remember to call it with:

Person.send("foo-bar")

I believe...?

M.T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,684
Latest member
sparada

Latest Threads

Top