Forum: Ruby I have some problem with convert to symbol, in Hash object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
51abb2c428788c12d4f3ea19aaa2b350?d=identicon&s=25 John (Guest)
on 2006-04-27 10:56
I use it functionality:

 >> Hash[*str.scan(/store\[([^\s,]+)\s*\]=\s*([^,]+)/).flatten]
=> {"fax"=>"80233923293", "name"=>"Ilyas store",
"description"=>"lkjklsdaj", "phone"=>"4999-233-2923"}

The problem with covert string to symbol.

I want to convert "name" to :symbol
  "name"=>"Ilyas store"   ---> :name => "Ilyas store"

------------------------------------------------------------------------
On Apr 26, 2006, at 7:12 AM, Jonh wrote:

>
> if params[:param]
>          paramater = Hash.new
>          for value in params[:param].split(/,/)
>              if value =~ /(.*)=(.*)/
>                 parameter[$1] = $2
>              end
>          end
> end
> puts parameter.inspect

Does this help?

 >> str = "store[name]=Ilyas store,store[phone]= 4999-233-2923, store
[fax]= 80233923293,store[description]=lkjklsdaj,save=save,cancel=cancel"
=> "store[name]=Ilyas store,store[phone]= 4999-233-2923, store[fax]=
80233923293,store[description]=lkjklsdaj,save=save,cancel=cancel"
 >> Hash[*str.scan(/([^\s,]+)\s*=\s*([^,]+)/).flatten]
=> {"cancel"=>"cancel", "store[fax]"=>"80233923293", "store
[name]"=>"Ilyas store", "save"=>"save", "store
[description]"=>"lkjklsdaj", "store[phone]"=>"4999-233-2923"}

James Edward Gray II
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2006-04-27 12:58
(Received via mailing list)
2006/4/27, John <iiskakov@issart.com>:
> I use it functionality:
>
>  >> Hash[*str.scan(/store\[([^\s,]+)\s*\]=\s*([^,]+)/).flatten]
> => {"fax"=>"80233923293", "name"=>"Ilyas store",
> "description"=>"lkjklsdaj", "phone"=>"4999-233-2923"}
> The problem with covert string to symbol.
>
> I want to convert "name" to :symbol
>   "name"=>"Ilyas store"   ---> :name => "Ilyas store"


Difficult to test without the original string but try something like
this:

Hash[*str.scan(/store\[([^\s,]+)\s*\]=\s*([^,]+)/).map{|k, v|
[k.to_sym,v]}.flatten]

Kind regards

robert
This topic is locked and can not be replied to.