When you make a string…what does it mean to “format” that string?
CHALLENGE: Explain this to me like I’m an absolute idiot. Like, take it to a condescending level. Be mean about it. I’m talking how you would explain a lemonade stand to a very, very stupid child.
The most common use of the phrase refers to the replacing of variable placeholders within a string with the correct string representations of the variables’ contents.
temp_reading = 25.67528 puts "It is currently %0.1f degrees" % [temp_reading] -> It is currently 25.7 degrees
String formatting is what turns the template into the string you see in the output.
As pointed out by Phil Taprogge, typically formatting a string refers to changing the representation of data for presentation reasons.
long_number = 1.11111111111111111111111111111111111111111111111111111111111 puts "%0.1f" % long_number => 1.1 puts "%d" % long_number => 1
There is tons of documentation on string formatting and typically it can carry over to different languages since this come from the C programming language
Formatting a string could refer, however, to any and all transformations to a string for presentation.
str = "hello world" str.downcase => "hello world" str.upcase => "HELLO WORLD" str.capitalize => "Hello world" str.titlieze => "Hello World" str.parameterize => "hello-world"