0

Thanks for your time!

I get a string, looks like this :

web_custom_request("pricing_approval",
    "URL=http://loanoriginationci:8080/ro-web/service/pricing/task/list/pricing_approval?uniqueId=1362015883531",
    "Method=GET",
    "Resource=0",
    "RecContentType=text/xml",
    "Referer=http://loanoriginationci:8080/MAUIWeb/MAUIShell.swf/[[DYNAMIC]]/6",
    "Snapshot=t76.inf",
    "Mode=HTTP",
    LAST);

I want to get the parameters of this function string, and store them in different variables. Then what comes to my mind is splitting this string by string.split(","), then get each part of it.

But if there's a comma within the parameters, say this one "Body=xxxx,xxxx", the method above will be wrong.

So is there some elegant and neat way to deal with it? Thanks again!

mCY
  • 2,731
  • 7
  • 25
  • 43

2 Answers2

1

You can do this using parameters. Example below returns you a hash whose keys are local variable names and values are the values passed in method call.

def web_custom_request(a,b,c,d,e,f,g,h,i)
  Hash[*method(__method__).parameters.map { |arg| arg[1] }.map { |arg| [arg.to_s, "#{eval arg.to_s}"] }.flatten]
end

h = web_custom_request("pricing_approval",
                       "URL=http://loanoriginationci:8080/ro-web/service/pricing/task/list/pricing_approval?uniqueId=1362015883531",
                       "Method=GET",
                       "Resource=0",
                       "RecContentType=text/xml",
                       "Referer=http://loanoriginationci:8080/MAUIWeb/MAUIShell.swf/[[DYNAMIC]]/6",
                       "Snapshot=t76.inf",
                       "Mode=HTTP",
                       "LAST");

puts h # {"a"=>"pricing_approval", "b"=>"URL=http://loanoriginationci:8080/ro-web/service/pricing/task/list/pricing_approval?uniqueId=1362015883531", "c"=>"Method=GET", "d"=>"Resource=0", "e"=>"RecContentType=text/xml", "f"=>"Referer=http://loanoriginationci:8080/MAUIWeb/MAUIShell.swf/[[DYNAMIC]]/6", "g"=>"Snapshot=t76.inf", "h"=>"Mode=HTTP", "i"=>"LAST"}
saihgala
  • 5,724
  • 3
  • 34
  • 31
  • Great! It's really helpful. Here's another article may help to understand the codes here(honestly, the codes surpass my knowledge on Ruby)[is-there-a-way-to-access-method-arguments-in-ruby](http://stackoverflow.com/questions/9211813/is-there-a-way-to-access-method-arguments-in-ruby). Plus, as the function i deal with is a string, so I use `h = eval string` in my case. Thanks @Ashish – mCY Feb 28 '13 at 08:16
  • @user1476512 Thanks for adding that link. It appears that even I referred to that answer when faced with a problem similar to yours. – saihgala Feb 28 '13 at 09:03
0

I think it depends on how your string is formatted.

If it is as you have given above, then using string.split(without specifying a delimeter) will work because it will split the string where the white space is which in your case fall naturally between the different "parameters".

If you have no spacing at all in your string, i.e. something like:

string = 'web_custom_request("pricing_approval","Method=GET","Body=xxxx,xxxx")'

then you could use a regex to find the parts between the quotation marks such as string.scan(/"([^"]*)"/)

which gives the following match groups:

[["pricing_approval"], ["Method=GET"], ["Body=xxxx,xxxx"]]
simonb83
  • 108
  • 6
  • Damn! Thought I made it sound too easy. – simonb83 Feb 28 '13 at 04:50
  • In that case (and again in the scenario where there is no whitespace) maybe seek to split the string where a comma is directly next to a quotation mark, i.e. `string.split(/",/)`. – simonb83 Feb 28 '13 at 04:57