RichardOnRails
2010-May-08 23:24 UTC
Using re to make BidDecimal''s more readable using Regexp: problem
Hi, I''m on a mission to show my granddaughter how Pi can be computed. For that, I need Sqrt(2) computed accurately, which I''ve done. But I need to display results in 5-digit groups, space separated. The following aims at displaying the five-or-less decimal components of a 12-decimal-digit number (related to sqrt(2) but ignoring scaling and precision for the moment). bd = BigDecimal("0.141421356237") group_sz=5 re_string = "\\.(\\d{1," + group_sz.to_s + "})+" r = Regexp.new(re_string) m = r.match(bd.to_s) (0..3). each do |i| puts( "%s => %s; m[%d] => %s" % [r, m, i, m[i]] ) I get (ignoring scaling for the moment): (?-mix:\.(\d{1,5})+) => .141421356237; m[0] => .141421356237 (?-mix:\.(\d{1,5})+) => .141421356237; m[1] => 37 # want 14142 (?-mix:\.(\d{1,5})+) => .141421356237; m[2] => # want 13562 (?-mix:\.(\d{1,5})+) => .141421356237; m[3] => # want 37 So the extra set of parentheses I added don''t capture the way I want them to. I can do it using Ruby to generate a bunch of consecutive \d{1,5} groups to satisfy my requirement, but some insightful Regexp markup is preferable. Any ideas? Thanks in Advance, Richard -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rick DeNatale
2010-May-09 00:19 UTC
Re: Using re to make BidDecimal''s more readable using Regexp: problem
On Sat, May 8, 2010 at 7:24 PM, RichardOnRails <RichardDummyMailbox58407-gP6xRNRnnqSxhq/XJNNIW0EOCMrvLtNR@public.gmane.org> wrote:> Hi, > > I''m on a mission to show my granddaughter how Pi can be computed. For > that, I need Sqrt(2) computed accurately, which I''ve done. But I need > to display results in 5-digit groups, space separated. > > The following aims at displaying the five-or-less decimal components > of a 12-decimal-digit number (related to sqrt(2) but ignoring scaling > and precision for the moment). > > bd = BigDecimal("0.141421356237") > group_sz=5 > re_string = "\\.(\\d{1," + group_sz.to_s + "})+" > r = Regexp.new(re_string) > m = r.match(bd.to_s) > (0..3). each do |i| > puts( "%s => %s; m[%d] => %s" % [r, m, i, m[i]] ) > > I get (ignoring scaling for the moment): > > (?-mix:\.(\d{1,5})+) => .141421356237; m[0] => .141421356237 > (?-mix:\.(\d{1,5})+) => .141421356237; m[1] => 37 # want 14142 > (?-mix:\.(\d{1,5})+) => .141421356237; m[2] => # want 13562 > (?-mix:\.(\d{1,5})+) => .141421356237; m[3] => # want 37 > > So the extra set of parentheses I added don''t capture the way I want > them to. I can do it using Ruby to generate a bunch of consecutive > \d{1,5} groups to satisfy my requirement, but some insightful Regexp > markup is preferable. > > Any ideas?Does this help? BigDecimal("0.141421356237").to_s.scan(/(\d{1,5}|\.|\E)/).join(" ") #=> "0 . 14142 13562 37 E 0" -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
hi, im having a model called category (polymorohic) and listing it in the index-view: <table> <tr> <th>Title engl</th> </tr> <% @categories.each do |category| %> <tr> <td><%= link_to h(category.title_engl), category %> - #of Subcategories: <%=h category.children.count %> - #of questions in that category :<%=h category.messages.count %><% category.children.each do |cat| %> <%=h cat.title_engl %><br> //OK <% end %> <hr> <% for index in 0 ... 5 %> <%=h category.children[index].inspect %><br> <% end %> </td> </tr> <% end %> </table>> as u can see, in the for-loop i want for example show only 5 childrenof that specific children. inspect gives me the correct values, but i cant address them. whats the proper way here? thx -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2010 02:17, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hi, im having a model called category (polymorohic) and listing it in > the index-view: > <table> > <tr> > <th>Title engl</th> > </tr> > > <% @categories.each do |category| %> > <tr> > <td><%= link_to h(category.title_engl), category %> > - #of Subcategories: <%=h category.children.count %> > - #of questions in that category :<%=h category.messages.count % >> > > <% category.children.each do |cat| %> > <%=h cat.title_engl %><br> //OK > <% end %> > > <hr> > <% for index in 0 ... 5 %> > <%=h category.children[index].inspect %><br> > <% end %> > > > </td> > </tr> > <% end %> > </table> > >> as u can see, in the for-loop i want for example show only 5 children > of that specific children. inspect gives me the correct values, but i > cant address them. whats the proper way here?I don''t understand "inspect gives me the correct values, but i cant address them". If inspect is working what is that is not working? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2010 02:17, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> <% for index in 0 ... 5 %> > <%=h category.children[index].inspect %><br> > <% end %>>> as u can see, in the for-loop i want for example show only 5 children > of that specific children. inspect gives me the correct values, but i > cant address them. whats the proper way here?First off, consider what will happen if your category has less than five children (although you''re looping through six!); you''ll get a nil value, and all the child.attribute stuff you''ll want to do will break. So I''d suggest that instead of accessing by index, you iterate a slice of your collection:> <% category.children.slice(0..4) do |child| %> > <%=h child.inspect %><br> > <% end %>Then, what''s the error you''re getting when you replace "child.inspect" with "child.attribute"? (I have a feeling it might have been a "no method ''attribute'' for nilclass...", which won''t occur now you''re iterating only existing children, but that''s just a hunch ;-) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2010 09:35, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 May 2010 02:17, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> <% for index in 0 ... 5 %>> you''re looping through sixSorry... three dots... stupid confusing functionality :-/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
hi guys & thx for the suggestions 1) slice: didnt know u can use that on "collections" too, so i give it a try. the boundaries are being checked to not run into a nil situation 2) based on my snippet, insoect works, but if i want to address the attribute like: <%=h category.children[index].id %><br> gives me: Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id not sure how to access or whats actually wrong.... thx again! On Sun, May 9, 2010 at 4:35 AM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 May 2010 02:17, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > <% for index in 0 ... 5 %> > > <%=h category.children[index].inspect %><br> > > <% end %> > > >> as u can see, in the for-loop i want for example show only 5 children > > of that specific children. inspect gives me the correct values, but i > > cant address them. whats the proper way here? > > First off, consider what will happen if your category has less than > five children (although you''re looping through six!); you''ll get a nil > value, and all the child.attribute stuff you''ll want to do will break. > So I''d suggest that instead of accessing by index, you iterate a slice > of your collection: > > > <% category.children.slice(0..4) do |child| %> > > <%=h child.inspect %><br> > > <% end %> > > Then, what''s the error you''re getting when you replace "child.inspect" > with "child.attribute"? (I have a feeling it might have been a "no > method ''attribute'' for nilclass...", which won''t occur now you''re > iterating only existing children, but that''s just a hunch ;-) > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2010 09:54, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 2) based on my snippet, inspect works"works" .... any chance one of the "working" results is "nil" :-/> <%=h category.children[index].id %><br> > > gives me: > > Called id for nil, which would mistakenly be 4 -- if you really wanted the > id of nil, use object_idExactly...> 1) slice: didnt know u can use that on "collections" too, so i give it a > try.Do.> the boundaries are being checked to not run into a nil situationI beg to differ... given the "called id for nil" message... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
strange, slice doesnt give me anything...: <% category.children.slice(0...4) do |child| %> test <%=h child.inspect %><br> <% end %> i see no "test" ini the resulting view. children are present running the other loop. and even if it would work, would it prevent nil@out-ofboundary? thx -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ok, got it: <% category.children.slice(0...4).EACH do |child| %>> i forgot the "each" keyword.thx again! On Sun, May 9, 2010 at 5:11 AM, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> strange, slice doesnt give me anything...: > > <% category.children.slice(0...4) do |child| %> > test <%=h child.inspect %><br> > <% end %> > > i see no "test" ini the resulting view. children are present running the > other loop. > and even if it would work, would it prevent nil@out-ofboundary? > > thx > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 May 2010 10:11, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> and even if it would work, would it prevent nil@out-ofboundary?Try it yourself - explore and find out. Go the the console and play with arrays. Read the api docs which explain what it does. http://ruby-doc.org/core/classes/Array.html#M002205 You''ll learn more about it than just being told if you discover the limits yourself (and you can test it with your own collections to be sure, rather than just relying on my word for it). -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
ur right. thx again 4 ur words! On Sun, May 9, 2010 at 5:25 AM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 May 2010 10:11, tom <tomabroad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > and even if it would work, would it prevent nil@out-ofboundary? > > Try it yourself - explore and find out. Go the the console and play > with arrays. Read the api docs which explain what it does. > > http://ruby-doc.org/core/classes/Array.html#M002205 > > You''ll learn more about it than just being told if you discover the > limits yourself (and you can test it with your own collections to be > sure, rather than just relying on my word for it). > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.