Ad

What are all the ways to output a string in Ruby?

Code
Diff
  • # What are all the ways to output a string in Ruby?
    def hello_ruby
      greet = "Hello Ruby!"
    
      print           greet, "\n"
      puts            greet
      $stdout.write   greet + "\n"
      $stdout.puts    greet
      $stdout.print   greet, "\n"
      $stdout <<      greet + "\n"
      (greet+"\n").each_char {|c| print c}
    end
    • greet = "Hello Ruby!"
    • # What are all the ways to output a string in Ruby?
    • def hello_ruby
    • greet = "Hello Ruby!"
    • print greet, "
    • "
    • puts greet
    • $stdout.write greet +"
    • "
    • $stdout.puts greet
    • $stdout.print greet, "
    • "
    • (greet+"
    • ").each_char {|c| print c}
    • print greet, "
    • "
    • puts greet
    • $stdout.write greet + "
    • "
    • $stdout.puts greet
    • $stdout.print greet, "
    • "
    • $stdout << greet + "\n"
    • (greet+"
    • ").each_char {|c| print c}
    • end