Hi, I love ruby,but it seems Ruby don''t me. :) I actually doing some play with nokogiri methods since last 3 days. Doing so I have ended up with the below code: require ''nokogiri'' doc = Nokogiri::XML(<<-eohl) <Stock> <Code>7052</Code> <Name>PADINI</Name> <StockDailyRecords> <PriceOpen>1.2</PriceOpen> <PriceChange>1.1</PriceChange> <PriceClose>10.0</PriceClose> <Volume>3000000L</Volume> </StockDailyRecords> <StockDailyRecords> <PriceOpen>1.3</PriceOpen> <PriceChange>1.2</PriceChange> <PriceClose>11.0</PriceClose> <Volume>5000000L</Volume> </StockDailyRecords> </Stock> eohl doc.at_css("StockDailyRecords").children.map{|el| el.text.strip if el.text.strip != "" }.compact # => ["1.2", "1.1", "10.0", "3000000L"] but my code not seems to be a Rubyistic. Any good approach would anybody give me? :))) -- 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/06c7f08ef74661fd157133b8daf0c6ef%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Love U Ruby <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I actually doing some play with nokogiri methods since last 3 days. > Doing so I have ended up with the below code: > > require ''nokogiri'' > > doc = Nokogiri::XML(<<-eohl) > <Stock> > <Code>7052</Code> > <Name>PADINI</Name> > <StockDailyRecords> > <PriceOpen>1.2</PriceOpen> > <PriceChange>1.1</PriceChange> > <PriceClose>10.0</PriceClose> > <Volume>3000000L</Volume> > </StockDailyRecords> > <StockDailyRecords> > <PriceOpen>1.3</PriceOpen> > <PriceChange>1.2</PriceChange> > <PriceClose>11.0</PriceClose> > <Volume>5000000L</Volume> > </StockDailyRecords> > </Stock> > eohl > > doc.at_css("StockDailyRecords").children.map{|el| el.text.strip if > el.text.strip != "" }.compact > # => ["1.2", "1.1", "10.0", "3000000L"] > > but my code not seems to be a Rubyistic. Any good approach would anybody > give me? :)))I actually do not have a clue what you mean by that? Did you not get what you expected? If you did get what you expected, I don''t see anything wrong with your code.... There are other ways to do that, of course, but they''re not more idiomatic, if that''s what you mean, and can be much worse. This could work as well: doc.at_css("StockDailyRecords").children.map(&:text).map(&:strip).select{|el| not el.empty?} but it''s not *better*; It''s actually worse, IMO, because it''s rolling through the collection several times. Yet another: doc.at_css("StockDailyRecords").children.select do |el| el.text.strip!; el unless el.empty? end which has the side effect of changing the contents of doc, in my opinion is dangerous. And there''s inject/reduce, which can be even more arcance/opaque: doc.at_css("StockDailyRecords").children.reduce([]) do |m,o| o = o.text.strip m << o unless o.empty? m end Given time, I could probably come up with a few more. But that''s moot. Your code is fine. -- 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/51b68782.c572320a.3128.ffff8aef%40mx.google.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Find the below code : require ''nokogiri'' doc = Nokogiri::HTML(<<-eohl) <div> <dt> Test 1 </dt> <dd> </dd> <dt> Test 2 </dt> <dd> foo </dd> </div> eohl doc.search(''div'').children.reject(&:text?).map{|n| n.name if !n.child.to_str.strip.empty? }.compact # => ["dt", "dt", "dd"] Any short technique to generate the array `["dt", "dt", "dd"]` ? Thanks -- 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/50c806602f653550e47a22100e408b1d%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Love U Ruby wrote in post #1112338:> doc.search(''div'').children.reject(&:text?).map{|n| n.name if > !n.child.to_str.strip.empty? }.compact > # => ["dt", "dt", "dd"]I found one shortest way using `css` rules : doc.search("dt,dd").map{|n| n.name unless n.to_str.strip.empty? }.compact # => ["dt", "dt", "dd"] -- 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/7a2f729febb64d3ba6fe05f1572bb477%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Tamara Temple
2013-Jun-18 03:23 UTC
Re: Re: Good Rubyistic way Looking for nokogiri program
not sure what this has to do with rails.... -- 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/51bfd297.0d99320a.4eb7.53d2%40mx.google.com. For more options, visit https://groups.google.com/groups/opt_out.