Include a table but or a variable in a ruby string. To replace an expression or variable with a different value
1 Printf-style methods like Java or Python
Template ='Oceania have always been at the war with%s.'Template%'Eurasia' #= = "Oceania have always been at the war with Eurasia."Template%'Eastasia' #= = "Oceania have always been at the war with Eastasia."
'To 2 decimal places:%.2f'%Math::P i# = "to 2 decimal places:3.14"
'zero-padded:%.5d' %Math::P i #= "zero-padded:00003"
2 or use the ERB, because I use the IDE, so the last line uses the kernel.binding, if you are in IRB, you can only use the binding
' Erb ' = erb.new%q{chunky <%= food%>"bacon"= Template.result ( kernel.binding)
3 Flipping a string
s = .sdrawkcab si gnirts siht " s.reverse # = "This string is backwards." s # = ". Sdrawkcab si gnirts siht" s.reverse! # = "This string is backwards." s # = "This string is backwards."
" order. Wrong The in is words these " S.split (/(\s+)/). reverse!. Join ('# = ' These words is in the wrong order. " s.split (/\b/). reverse!. Join ('# = ' These words is in the wrong. Order "
(\s+) and \s+, (\s+) The matching spaces are included in the return list
" Three Little Words ". Split (/\s+/)# = [" Three "," little "," words "]"Three Little Words". Split (/(\s+)/)# + = [" Three "," "," Little "," "," words "]
Ruby String Learning 2