require ''json'' str = "1=2,3=(4=5,6=7)" new_str = str.gsub(/=|\(|\)/) do |m| if m == "=" m= "=>" elsif m == "(" m = "{" else m = "}" end end new_str = new_str.prepend("{") << "}" # => "{1=>2,3=>{4=>5,6=>7}}" JSON.parse(new_str) # ~> /home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/json/common.rb:155:in `parse'': 757: unexpected token at ''{1=>2,3=>{4=>5,6=>7}}'' (JSON::ParserError) # ~> from /home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/json/common.rb:155:in `parse'' # ~> from -:15:in `<main>'' My question is how should I get the hash {1=>2,3=>{4=>5,6=>7}} from that string? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/da8a33c74e789b2ef9d42b0da3aa5c6b%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Aug 26, 2013 at 12:42 PM, Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > My question is how should I get the hash {1=>2,3=>{4=>5,6=>7}} from that > string?Why don''t you generate valid JSON in the first place? `{1=>2,3=>{4=>5,6=>7}}.to_json` would show you what the string should look like (and why your example is so broken). -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yAtQin7%3DFNPWxaemBjktJkU3-aWzCNLGOhqJkDTmZhm2Q%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder wrote in post #1119612:> On Mon, Aug 26, 2013 at 12:42 PM, Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >> >> My question is how should I get the hash {1=>2,3=>{4=>5,6=>7}} from that >> string? > > Why don''t you generate valid JSON in the first place? > > `{1=>2,3=>{4=>5,6=>7}}.to_json` would show you what the string > should look like (and why your example is so broken).Humm.. its working now.. require ''json'' JSON.parse({1=>2,3=>{4=>5,6=>7}}.to_json) # => {"1"=>2, "3"=>{"4"=>5, "6"=>7}} -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e69b59d116935cd2bdbc7e966225009a%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Hassan Schroeder wrote in post #1119612:> On Mon, Aug 26, 2013 at 12:42 PM, Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> > wrote: >> >> My question is how should I get the hash {1=>2,3=>{4=>5,6=>7}} from that >> string? > > Why don''t you generate valid JSON in the first place? > > `{1=>2,3=>{4=>5,6=>7}}.to_json` would show you what the string > should look like (and why your example is so broken).In my case the hash is inside the string,but I want the hash back from the string? :( I don''t know how to do this... -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/9bda948da535446a2d4499e4dde12c62%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Aug 26, 2013 at 1:12 PM, Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> In my case the hash is inside the string,but I want the hash back from > the string? :( I don''t know how to do this...If you must use that format -- 2.0.0-p247 :017 > str => "1=2,3=(4=5,6=7)" 2.0.0-p247 :018 > eval str.tr(''()'',''{}'').gsub(/=/,''=>'').prepend(''{'').concat(''}'') => {1=>2, 3=>{4=>5, 6=>7}} 2.0.0-p247 :019 > -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yCmMq8D-FDgn3asD4ADDbZL3mSh-cxKWP_55V7D781dOg%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On Aug 26, 2013, at 2:42 PM, Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> require ''json'' > > str = "1=2,3=(4=5,6=7)" > new_str = str.gsub(/=|\(|\)/) do |m| > if m == "=" > m= "=>" > elsif m == "(" > m = "{" > else > m = "}" > end > end > > new_str = new_str.prepend("{") << "}" > # => "{1=>2,3=>{4=>5,6=>7}}" > > JSON.parse(new_str) > # ~> > /home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/json/common.rb:155:in > `parse'': 757: unexpected token at ''{1=>2,3=>{4=>5,6=>7}}'' > (JSON::ParserError) > # ~> from > /home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/json/common.rb:155:in > `parse'' > # ~> from -:15:in `<main>'' > > > My question is how should I get the hash {1=>2,3=>{4=>5,6=>7}} from that > string? > > -There is no JSON here at all. Given this:> new_str = new_str.prepend("{") << "}" > # => "{1=>2,3=>{4=>5,6=>7}}"You have a string which represents a what a Hash looks like. Why not run it through eval and see what you get? my_hash = eval(new_str) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/D83E564F-33DE-4060-B4C5-8E010AE04835%40gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
tamouse m. wrote in post #1119628:> On Aug 26, 2013, at 2:42 PM, Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > You have a string which represents a what a Hash looks like. Why not run > it through eval and see what you get? > > my_hash = eval(new_str)I was trying to avoid `eval`. I tried also the `yaml`. But no luck :( -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e783312f4f38da2b6ade8a29eb1b53e8%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
On 2013-Aug-26, at 17:10 , Love U Ruby wrote:> tamouse m. wrote in post #1119628: >> On Aug 26, 2013, at 2:42 PM, Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> >> You have a string which represents a what a Hash looks like. Why not run >> it through eval and see what you get? >> >> my_hash = eval(new_str) > > I was trying to avoid `eval`. I tried also the `yaml`. But no luck :(If all your keys and values are just digits (i.e., String#to_i likes them), then you can easily avoid eval and yaml. Just make your own parser. Here are some "learning tests" to get you started. If you avoid scrolling down too far to see my "answer", then just make the tests pass and you''re done! Of course, you can also expand the tests to cover even more exotic hash keys and values, but if you go too far you''ve just reinvented YAML or JSON with different syntax. -Rob require ''minitest/autorun'' class TestSomeCrappyMarkupLanguage < Minitest::Test def setup @str = "1=2,3=(4=5,6=7)" end def test_nil refute SomeCrappyMarkupLanguage.parse(nil) end def test_empty_string assert_equal({}, SomeCrappyMarkupLanguage.parse("")) end def test_simple_hash assert_equal({1=>2}, SomeCrappyMarkupLanguage.parse("1=2")) end def test_two_elements assert_equal({4=>5,6=>7}, SomeCrappyMarkupLanguage.parse("4=5,6=7")) end def test_nested expected = { 1 => 2, 3 => { 4 => 5, 6 => 7 } } assert_equal expected, SomeCrappyMarkupLanguage.parse(@str) end end # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... # spoilers below... class SomeCrappyMarkupLanguage def self.parse(str) return nil unless str result = {} str.scan(/(\d+)=((?:\([^\)]*\))|\d+),?/).each do |key,value| key = key.to_i value = value =~ /\A\d+\z/ ? value.to_i : parse(value) result[key] = value end result end end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/B705512C-FD76-4F45-BE2D-81DD0904DCA0%40agileconsultingllc.com. For more options, visit https://groups.google.com/groups/opt_out.
Rob Biedenharn wrote in post #1119642: @Rob - thanks for such an detailed answer.. :) I need to give some more time on this...> class SomeCrappyMarkupLanguage > def self.parse(str) > return nil unless str > result = {} > str.scan(/(\d+)=((?:\([^\)]*\))|\d+),?/).each do |key,value| > key = key.to_i > value = value =~ /\A\d+\z/ ? value.to_i : parse(value) > result[key] = value > end > result > end > end-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/97ea149d21a7824e88d7c93d5e845323%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.