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

On Sat, May 8, 2010 at 7:24 PM, RichardOnRails
[email protected] wrote:

bd = BigDecimal(“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: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

hi, im having a model called category (polymorohic) and listing it in
the index-view:

<% @categories.each do |category| %>


Title engl
<%= 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 %>

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?
thx

On 9 May 2010 02:17, tom [email protected] wrote:

   - #of Subcategories: <%=h category.children.count %>
   <% end %>


 </td>
<% 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?

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

On 9 May 2010 02:17, tom [email protected] 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 :wink:

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 %>

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 9 May 2010 09:35, Michael P. [email protected] wrote:

On 9 May 2010 02:17, tom [email protected] wrote:

   <% for index in 0 ... 5 %>

you’re looping through six

Sorry… three dots… stupid confusing functionality :-/

On 9 May 2010 09:54, tom [email protected] wrote:

  1. based on my snippet, inspect works

“works” … any chance one of the “working” results is “nil” :-/

<%=h category.children[index].id %>

gives me:

Called id for nil, which would mistakenly be 4 – if you really wanted the
id of nil, use object_id

Exactly…

  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 situation

I beg to differ… given the “called id for nil” message…

strange, slice doesnt give me anything…:
<% category.children.slice(0…4) do |child| %>
test <%=h child.inspect %>

<% 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

ok, got it:
<% category.children.slice(0…4).EACH do |child| %>

i forgot the “each” keyword.

thx again!

ur right. thx again 4 ur words!

On 9 May 2010 10:11, tom [email protected] 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).