Leading Zeros in a Method

I found the following topic in the forum:
http://www.ruby-forum.com/topic/82137

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


“%05d” % an_int


I’m still new to ruby, so I wanted to know. How would I put this into a
method that I can attach to whatever string I wanted.

I have the following in the view of my Rails app:


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


I want to be able to create a method that will add the leading zeros to
the order.id part. I know I’ll have to do something like:

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

I know how to create a method. I just don’t know how to put that code
(“%05d” % an_int) into the leading_zeros method and be able to attach it
to things.

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.

On 1 June 2010 15:44, Matt R. [email protected] wrote:

I want to be able to create a method that will add the leading zeros to
the order.id part. I know I’ll have to do something like:

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

I know how to create a method. I just don’t know how to put that code
(“%05d” % an_int) into the leading_zeros method and be able to attach it
to things.

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

Peter H. 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}"

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

Any ideas? Thanks again for all your help.

Matt R. 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

Colin L. 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

Colin L. 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 R.

On 1 June 2010 16:18, Matt R. [email protected] wrote:

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

On 1 June 2010 16:20, Matt R. [email protected] wrote:

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?

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 L. 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 R.

Matt R. wrote:

Colin L. 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.