SonyMag
SonyMag

Reputation: 171

How to puts arguments in one line?

I want to make it like in Python, 4 example:

print(
    foobar,
    footree,
    foodrink
)

and it will be print in one line:

>>> "foobar, footree, foodrink"

In Ruby with the same code:

puts foobar,
     footree,
     foodrink

>>> "foobar"
... "footree"
... "foodrink" 

Yes, I can do it with print, like this, but it looks ugly:

puts  "foobar" +
      "foobar" +
      "foobar" +

>>> "foobar, footree, foodrink"

Thx in advance!

enter image description here

Edited. Now I have the following "Align the arguments of a method call if they span more than one line" and in terminal it output from a new line, I need it in one line.

Upvotes: 1

Views: 320

Answers (3)

user16452228
user16452228

Reputation:

I'm making some assumptions about your various data types based on limited information given in your example, but the closest syntax I can come up with is using an array([]) along with p and join like so:

foobar = "foobar"
footree = "footree"
foodrink = "foodrink"

p [
    foobar,
    footree,
    foodrink
].join(" ")
#=>  "foobar footree foodrink"

Upvotes: 1

Alex
Alex

Reputation: 29766

>> a, b = "bar", "tree"
>> puts [a, b].join(", ")
bar, tree

or print:

>> print a, ", ", b, "\n"
bar, tree

Upvotes: 1

Easyjobber
Easyjobber

Reputation: 46

You have to do

puts "#{foobar}, #{footree}, \
#{foodrink}"

Upvotes: 2

Related Questions