never_had_a_name never_had_a_name - 11 days ago 4
Ruby Question

Add whitespace before and after a string in ruby?

I want to add a whitespace before and after random strings.

I have tried using "Random_string".center(1, " ") but it doesnt work.

Thanks

Answer

My ruby is rusty, but IMO nothing wrong with the easy way

def pad( random )
    " " + random + " "
end

padded_random_string = pad("random_string")

using center

"random_string".center( "random_string".length + 2 )
Comments