Pregunta

I am using rjust in a helper function as follows:

def get_child_id(service_name, child_id)
  ..
  return 'FL' + child_id.to_s.rjust(6, "0");
end

The function is being called like so:

<%= get_child_id(@ticket.service_type, @ticket.service_id) %>

and returns FL123456 (123456 being the database value) whereas I would expect FL000000123456

The service_id is an INT column in the database.

I use rjust in my models in the exact same way and it works fine so I'm at a loss why it's not working here, any ideas?

¿Fue útil?

Solución

use 12 instead:

def get_child_id(service_name, child_id)
  # ...
  return 'FL' + child_id.to_s.rjust(12, "0");
end

Otros consejos

try this out

from ruby doc:-

 rjust(integer, padstr=' ') → new_str

If integer is greater than the length of str, returns a new String of length integer with str right justified and padded with padstr; otherwise, returns str.

"hello".rjust(4)            #=> "hello"
"hello".rjust(20)           #=> "               hello"
"hello".rjust(20, '1234')   #=> "123412341234123hello"

it means you want a padding of more than 6 in your case 12.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top