In the following ruby code, the output becomes: "x y"
x = "x %s"
y = "y"
z = "z"
print x % y %z
The %z is ignored.
I want the output to be "x y z".
To understand the possibilities and limitations of the syntax of Ruby, I want to use only the print command and the %s and % flags. I know how to do this using sprintf but I want to know if there's a way to do it with just the 'print' command.
So I tried adding a second %s to the definition of the variable 'x':
x = "x %s %s"
y = "y"
z = "z"
print x % y %z
But I got this error:
in `%': too few arguments (ArgumentError)
With sprintf you could do this:
x = "x %s %s"
y = "y"
z = "z"
print (sprintf(x, y, z))
The output would be what I wanted:
x y z
But that's just too easy.
Is there a way to do this with just 'print' (not sprintf) and without using #{} or combining y and z into an array as [y,z]?