Leading Zeros in a Method

I found the following topic in the forum:

Ian J Cottee had an easy way of adding leading zeros to a number:

Rather than attempt to add a method to the String class, which is what you seem to be suggesting, You could add a method to your model that returns the id and name as a formatted string. So in order.rb something like def order_code   "#{'%05d' % id} #{name}" end Then in the view just use <%= @order.order_code %>

Colin

you could create a method called

def leading_zeros(number)

“%05d” % number

end

and then call it

“#{leading_zeros order.id}#{order.name}”

What you have with “”#{order.id}".leading_zeros

is that #{order.id} will evaluate to a string and then you are trying to call the leading_zeros method on the string. For it to work as you intend leading_zeros needs to be part of the String class.

There is no end of pain in doing this.

Just save leading_zeros as a helper method in the application helps and call it as I have shown.

Peter Hickman wrote:

you could create a method called

def leading_zeros(number)   "%05d" % number end

and then call it

"#{leading_zeros order.id}#{order.name}"

Thanks Peter!

I put the method in the application_helper.rb file:

Colin Law wrote:

Rather than attempt to add a method to the String class, which is what you seem to be suggesting, You could add a method to your model that returns the id and name as a formatted string. So in order.rb something like def order_code   "#{'%05d' % id} #{name}" end Then in the view just use <%= @order.order_code %>

Hi Colin,

Would this work if I wanted to put the leading zeros elsewhere in my app (other views tied to other models and controllers)? Would I just reference the Order model and then do this?

Thanks for your help,

--Matt

Peter Hickman wrote:

you could create a method called

def leading_zeros(number) "%05d" % number end

and then call it

"#{leading_zeros order.id}#{order.name}"

Thanks Peter!

I put the method in the application_helper.rb file:

---- def leading_zeros(number) "%05d" % number end ----

And then put the following like you said:

---- #{leading_zeros order.id} #{order.name}"

You have a missing " on the front, but that may just be an copy/paste error

----

It's giving me this error when I refresh my view:

---- NoMethodError in Orders#index Showing app/views/orders/index.html.erb where line #9 raised:

undefined method `leading_zeros' for "00009":String

Are you sure line 9 of that file is the one you have shown above? Have you remembered to save the file?

Colin

Matt Royer wrote:

Thanks Peter!

I put the method in the application_helper.rb file:

----     def leading_zeros(number)       "%05d" % number     end ----

And then put the following like you said:

---- #{leading_zeros order.id} #{order.name}" ----

It's giving me this error when I refresh my view:

---- NoMethodError in Orders#index Showing app/views/orders/index.html.erb where line #9 raised:

undefined method `leading_zeros' for "00009":String ----

Sorry Peter,

This is working great! I forgot to take the .leading_zeros off of the end of the string. Dumb mistake.

Thanks again!!! This is working ferfectly.

--Matt

No, that an instance method of Order so will only work on Order objects. It will work in any view (associated with any controller) but only if you have an Order object.

Colin

Colin Law wrote:

Are you sure line 9 of that file is the one you have shown above? Have you remembered to save the file?

I forgot to take my previous solution out of the view. :slight_smile: Dumb mistake, I know.

Thanks Colin for you help!

--Matt Royer

Colin Law wrote:

No, that an instance method of Order so will only work on Order objects. It will work in any view (associated with any controller) but only if you have an Order object.

Oh, okay. Little by little (very little it seems so far) I'm getting the hang of this. Man, this stuff is daunting, but cool to work with.

Thanks again Colin.

--Matt Royer

Matt Royer wrote:

Colin Law wrote:

No, that an instance method of Order so will only work on Order objects. It will work in any view (associated with any controller) but only if you have an Order object.

Oh, okay. Little by little (very little it seems so far) I'm getting the hang of this. Man, this stuff is daunting, but cool to work with.

Thanks again Colin.

Personally, I like Colin's approach the best. Yes, while you might want to add leading zeros in various places the example you showed seems specific to instances of Order.

"#{order.id}".leading_zeros + "#{order.name}"

The implementation of this would be best hidden (encapsulated) inside the Order class. If you need other classes to implement similar behavior then implement something similar inside those classes as well. In my opinion this is similar to overriding the to_s method to provide class specific behavior.

def order_code   "#{'%05d' % id} #{name}" end Then in the view just use <%= @order.order_code %>

This seems to be a very clean implementation and would be my recommendation. Given the simplicity of the "%" formatter operator I see no need to re-implement it as a helper. Combining it with the addition of appending the name does make sense to factor into a method of the Order class.