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

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

On 7/28/06, Andrew K. [email protected] 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

On 7/28/06, Andrew K. [email protected] wrote:

{:username=>“me”,:password=>“pass”,:remote-ip=>request.remote_ip

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

You can have ‘-’ in a key name, you simply can’t have ‘-’ in a symbol
literal. You can create the proper symbol like so:

remote_ip = :“remote-ip”

-Gabe

Thanks a lot!

Sean O’halpin wrote:

On 7/28/06, Andrew K. [email protected] 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

Andrew K. wrote:

      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

On Sat, 29 Jul 2006, Daniel S. wrote:

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

-a

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

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 K. wrote:

Thanks a lot!

Sean O’halpin wrote:

On 7/28/06, Andrew K. [email protected] 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

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.

[email protected] wrote:

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

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

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.

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:inmember’
/app/models/msisdn_call.rb:4

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