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

Richard

Does this help?

BigDecimal("0.141421356237").to_s.scan(/(\d{1,5}|\.|\E)/).join(" ")
#=> "0 . 14142 13562 37 E 0"

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

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

``````   &lt;% for index in 0 \.\.\. 5 %&gt;
&lt;%=h  category\.children\[index\]\.inspect %&gt;&lt;br&gt;
&lt;% end %&gt;
``````

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

``````   &lt;% category\.children\.slice\(0\.\.4\) do |child| %&gt;
&lt;%=h  child\.inspect %&gt;&lt;br&gt;
&lt;% end %&gt;
``````

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

Sorry... three dots... stupid confusing functionality :-/

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!

``````

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_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!

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