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]] )
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.
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?
thx
<% 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
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
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!
Try it yourself - explore and find out. Go the the console and play
with arrays. Read the api docs which explain what it does.
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).