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