Answer the question
In order to leave comments, you need to log in
String interpolation in Ruby and the times, upto and downto methods. How to enter in a line?
There is a task. Output the word "Hi" to the console 10 times (from a new line) and append an exclamation mark to it. At each iteration, the number of Exclamation Points should increase by 1.
Example:
Hello!
Hey!!
Hey!!!
etc.
Wrote like this:
1.upto(10) {|x| puts"Hello #{x.times { print "!"}}"}
1.upto(10) {|x| puts "Hello#{"!"*x}"}
Answer the question
In order to leave comments, you need to log in
1. The expression #{ x.times... }
in double quotes was expanded in the first place, but there inside -- print
, how could it not be called. Hence the exclamation marks before Hello.
2. Any block in Ruby, including the one that #{ ... }
returns the last calculated value (the result of the call x.times { ... }
, in this case), therefore, after displaying the exclamation points and the Hello line, the interpreter substituted the result of its calculation instead of the expression.
3. The solution is already in the question.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question