never_had_a_name never_had_a_name - 1 year ago 93
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.


Answer Source

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

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

padded_random_string = pad("random_string")

using center

"random_string".center( "random_string".length + 2 )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download