Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Ruby string to n characters

How to force Ruby string variable output using puts to n characters so that if the variable is longer, it will be truncated, if shorter, it will be expanded by trailing or leading spaces?

Is there some standard method to do this?

like image 941
Paul Avatar asked Feb 05 '13 19:02

Paul


People also ask

How do you get the first n characters of a string in Ruby?

To access the first n characters of a string in ruby, we can use the square brackets syntax [] by passing the start index and length. In the example above, we have passed the [0, 3] to it. so it starts the extraction at index position 0 , and extracts before the position 3 .

What does \n do in Ruby?

In double quoted strings, you can write escape sequences and Ruby will output their translated meaning. A \n becomes a newline. In single quoted strings however, escape sequences are escaped and return their literal definition. A \n remains a \n .

What is string interpolation in Ruby?

String Interpolation, it is all about combining strings together, but not by using the + operator. String Interpolation works only when we use double quotes (“”) for the string formation. String Interpolation provides an easy way to process String literals.

How do you access characters in a string in Ruby?

Accessing Characters Within a String You can also access a single character from the end of the string with a negative index. -1 would let you access the last character of the string, -2 would access the second-to-last, and so on. This can be useful for manipulating or transforming the characters in the string.


2 Answers

Ruby supports using format strings, like many other languages:

 [11] (pry) main: 0> '%3.3s' % 'f' => "  f" [12] (pry) main: 0> '%3.3s' % 'foo' => "foo" [13] (pry) main: 0> '%3.3s' % 'foobar' => "foo" 

If you want to pad on the right, use a - in the format string:

 [14] (pry) main: 0> '%-3.3s' % 'f' => "f  " [15] (pry) main: 0> '%-3.3s' % 'foo' => "foo" [16] (pry) main: 0> '%-3.3s' % 'foobar' => "foo" 

You could also use printf or sprintf, but I prefer the more generic %, which is like sprintf.

From the sprintf docs:

   s   | Argument is a string to be substituted.  If the format       | sequence contains a precision, at most that many characters       | will be copied. 

and:

 -        | all           | Left-justify the result of this conversion. 

What if I want to pad not with spaces but with some other character?

The underlying Ruby code is written in C, and it's hard-coded to use ' ' as the pad character, so that's not possible directly, unless you want to modify the source, recompile your Ruby, and live with a one-off version of the language. Instead, you can do something like:

 [17] (pry) main: 0> ('%-3.3s' % 'f').gsub(' ', '.') => "f.." 

This gets a lot more complicated than a simple gsub though. Often it's better to adjust the string without using format strings if you need to supply special characters. Format strings are very powerful but they're not totally flexible.

Otherwise, I'd go with something like @steenslag's solution, which is the basis for rolling your own. You lose the ability to use format strings and have to rely on concatenating your strings or something similar to get columnar output, but it will also work.

like image 135
the Tin Man Avatar answered Sep 19 '22 23:09

the Tin Man


To make a string a fixed length shorter:

  str_var.slice(0..10) # starts at the first letter 

To make a string a fixed length longer:

  str_var.ljust(10, ' ') 

They could be combined into something like:

  (str_var.length > 10) ? str_var.slice(1..10) : str_var.ljust(10, ' ') 

Where str_var is, of course, your string

like image 40
MiGro Avatar answered Sep 22 '22 23:09

MiGro