[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Format question

Tom Ewall

5/3/2006 3:58:00 AM

I have a float which I'd like to show as 0 padded to three places, so
5.1 would show as 005.1
Here's my code:

<td><%= book.dewey %></td>

I have two questions. One is how to make the 5.1 display as 005.1, as
asked above. The second is, how could I find resources on line to
figure out how to do this, assuming there are such.

TIA

6 Answers

Robert Klemme

5/3/2006 7:42:00 AM

0

tewall@lycos.com wrote:
> I have a float which I'd like to show as 0 padded to three places, so
> 5.1 would show as 005.1
> Here's my code:
>
> <td><%= book.dewey %></td>

irb(main):005:0> "%05.1f" % 5.1
=> "005.1"
irb(main):006:0> sprintf "%05.1f", 5.1
=> "005.1"

> I have two questions. One is how to make the 5.1 display as 005.1, as
> asked above. The second is, how could I find resources on line to
> figure out how to do this, assuming there are such.

This is a good place to start, especially the Pickaxe
http://www.rub...

Kind regards

robert

Tom Ewall

5/3/2006 1:59:00 PM

0

That was a bit confusing because, just by blind luck (or unluck) 5.1 is
the same for the format and the number! This was a 10000 to 1 shot!
At any rate, that worked fine, and the online reference was exactly
what I was looking for. I'm not sure what the Pickaxe is though.
However this was very helpful:
http://www.ruby-doc.org/docs/Progra...

I'm sure I'll find other stuff as I look around, so thanks very much.

One other question I have is when doing Ruby on Rails, there must be
some way to make you code hook onto a class you've written separately.
That is, instead of something simple, like

<td><%= sprintf "%05.1f",book.category.dewey %></td>

I might want to reference as an object/method which I've created,
something like:

<td><%= Myobject.mymethod(book.category.dewey) %></td>

In Java I would handle this with an import. With ruby I'm assuming I
would write my code, save it with a .rb extension, and have it located
somewhere where it can be found. Is that correct?

Robert Klemme

5/3/2006 2:57:00 PM

0

tewall@lycos.com wrote:
> That was a bit confusing because, just by blind luck (or unluck) 5.1 is
> the same for the format and the number! This was a 10000 to 1 shot!

Right - didn't occur to me. That's funny.

> At any rate, that worked fine, and the online reference was exactly
> what I was looking for.

Great!

> I'm not sure what the Pickaxe is though.
> However this was very helpful:
> http://www.ruby-doc.org/docs/Progra...

*That* is the Pickaxe. :-))

> I'm sure I'll find other stuff as I look around, so thanks very much.

Yeah, you might even read if from start to end - you can also buy it as
book. In that case you'll even get the newer edition - much improved!

> One other question I have is when doing Ruby on Rails, there must be
> some way to make you code hook onto a class you've written separately.
> That is, instead of something simple, like
>
> <td><%= sprintf "%05.1f",book.category.dewey %></td>
>
> I might want to reference as an object/method which I've created,
> something like:
>
> <td><%= Myobject.mymethod(book.category.dewey) %></td>
>
> In Java I would handle this with an import. With ruby I'm assuming I
> would write my code, save it with a .rb extension, and have it located
> somewhere where it can be found. Is that correct?

I don't know Rails (shame on me) but you usually also need a line like
"require 'my_code'" somewhere. Likely Rails has some form of automation
if you put the file in certain places.

But maybe there are even different mechanisms for generic formatting.

Kind regards

robert

ekofoed

5/3/2006 3:28:00 PM

0

This worked in irb:

irb(main):004:0> "%05.1f" % 5.1
=> "005.1"

so perhaps you could try this

<td><%= "%05.1f" % book.dewey %></td>

regards

esk


tewall@lycos.com wrote:
> I have a float which I'd like to show as 0 padded to three places, so
> 5.1 would show as 005.1
> Here's my code:
>
> <td><%= book.dewey %></td>
>
> I have two questions. One is how to make the 5.1 display as 005.1, as
> asked above. The second is, how could I find resources on line to
> figure out how to do this, assuming there are such.
>
> TIA

Sam Smoot

5/4/2006 4:33:00 AM

0

Rails will auto-require files if they're in the $LOAD_PATH and the
constant name matches the file name.

So I could create a bob.rb file then, and put it in the ./lib folder
under my site. Then if I use: nice_guy = Bob.new somewhere, Rails will
use const_missing to automatically require bob.rb for you.

It has it's plusses and minuses I suppose. If you prefer to be
explicit, open up your environment.rb and add "require 'bob'" there
instead. You'll have to restart your webserver to see changes in
environment.rb take effect however (and maybe for non Controller or
Model files also, don't remember).

Tom Ewall

5/5/2006 11:19:00 PM

0

Thanks. Here's another rails question; how do I sort? I'm guessing
there's something you can put in the controller which will tell the SQL
to do an order by. Is that right?

What I'd like to do is click on a heading and have it sort by that.
Any ideas appreciated.