Format question

T

tewall

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
 
R

Robert Klemme

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.ruby-doc.org/

Kind regards

robert
 
T

tewall

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/ProgrammingRuby/

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

Robert Klemme

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/ProgrammingRuby/

*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
 
E

ekofoed

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
 
S

Sam Smoot

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).
 
T

tewall

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top