Format question

Discussion in 'Ruby' started by tewall@lycos.com, May 3, 2006.

  1. Guest

    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
     
    , May 3, 2006
    #1
    1. Advertising

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

    Kind regards

    robert
     
    Robert Klemme, May 3, 2006
    #2
    1. Advertising

  3. Guest

    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?
     
    , May 3, 2006
    #3
  4. 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/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
     
    Robert Klemme, May 3, 2006
    #4
  5. ekofoed Guest

    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


    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
     
    ekofoed, May 3, 2006
    #5
  6. Sam Smoot Guest

    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).
     
    Sam Smoot, May 4, 2006
    #6
  7. Guest

    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.
     
    , May 6, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,194
    Janaka
    Feb 18, 2004
  2. Andy
    Replies:
    7
    Views:
    6,317
    Roedy Green
    May 10, 2004
  3. shannon
    Replies:
    1
    Views:
    8,632
  4. shannon
    Replies:
    0
    Views:
    587
    shannon
    Feb 1, 2006
  5. Replies:
    6
    Views:
    479
    Thomas Ganss
    Jan 15, 2006
Loading...

Share This Page