EX 8 What does %{} do?


#1

OK I’m at the start of my Ruby journey, and I have been going through the exercises as the book suggests. But I have finally become undone. I really don’t understand the method %{} , what does it do??? in comparison to #{} ?? and how is it formatting the order in this exercise??

I tried going on but it seems that the %{} is very important.

Any help on this would be really helpful.

Rob


#2

You can quote strings and interpolate code within. You can also use modifiers right after the % .
Take a look here:
https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_.25_Notation


#3

formatter = “%{first} %{second} %{third} %{forth}”

puts formatter % {first: 1, second: 2, third: 3, forth: 4}
puts formatter % {first: “one” , second: “two”, third: “three”, forth: “four”}
puts formatter % {first: true, second: false, third: true, forth: false}
puts formatter % {first: formatter, second: formatter, third: formatter, forth: formatter}

puts formatter % {
first: “I had this thing.”,
second: “That you could type up right.”,
third: " But it didn’t sing.",
forth: “So I said goodnight.”


#4

That’s how you create a template where you can apply a hashmap for the contents. See how you are using {first: 1 to set the %{first} variable?