It's saying that you shouldn't use the "while true do ... end" construct if
you want an infinite loop, you should just use "loop do ... end". (Which is
correct; I'd forgotten about "loop" :))

Simply replace the first line with

result = loop do

and it should remove that warning. The second warning is that "do" is
actually redundant in a while loop (live and learn!); instead of

while condition do
  ...
end

you can simply say

while condition
  ...
end

The third warning is just a matter of correct indentation. The final one
hinges on a semantic difference in single versus double quoted ruby string
literals - a single-quoted string will literally reproduce its contents,
whereas a double-quoted one will perform string interpolation if you have a
#{} block within it, and interpret escape characters like \n.

$ irb
a = irb(main):001:0> a = 'hello'
=> "hello"
irb(main):002:0> b = '#{a} world'
=> "\#{a} world"
irb(main):003:0> c = "#{a} world"
=> "hello world"

There is no real reason to heed that warning - see
https://www.viget.com/articles/just-use-double-quoted-ruby-strings

martin

On Wed, Nov 30, 2016 at 3:42 AM, Nathan Guilty <ruby / e-solutions.re> wrote:

> Many thanks for all that reply to my question.
>
> So, i tried this (using the Martin's example):
>
> --8<--
>
> result = while true do
>          print 'Try? '
>          input = gets.chomp
>          exit if input == 'quit'
>          break input.to_i if input.to_i.to_s == input
>          puts "Please, a number."
>          end
>
> puts "You entered #{result}"
>
> -->8--
>
> It works, but checking with rubocop :
>
> --8<--
>
> debug.rb:1:1: C: Carriage return character detected.
> result = while true do ...
> ^^^^^^^^^^^^^^^^^^^^^^
> debug.rb:1:10: C: Use Kernel#loop for infinite loops.
> result = while true do
>          ^^^^^
> debug.rb:1:16: W: Literal true appeared in a condition.
> result = while true do
>                ^^^^
> debug.rb:1:21: C: Do not use do with multi-line while.
> result = while true do
>                     ^^
> debug.rb:2:10: C: Use 2 (not 0) spaces for indentation.
>          print 'Try? '
>
> debug.rb:6:15: C: Prefer single-quoted strings when you don't need string
> interpolation or special symbols.
>          puts "Please, a number."
>               ^^^^^^^^^^^^^^^^^^^
> debug.rb:10:1: C: 1 trailing blank lines detected.
>
> 1 file inspected, 7 offenses detected
>
> -->8--
>
> Can you help me to troubleshoot this ?
> I use also codebeat to review code, it accepts less than 10 lines per
> method.
> How can i refactor that ?
>
> Thank you very much for all your replies.
>
> Regards,
>
> /Nathan
>
>
> Le 2016-11-30 12:14, Martin DeMello a crit :
>
> The 'break value' statement is another option:
>>
>> result = while true do
>> print "Enter a number: "
>> input = gets.chomp
>> if input == "quit"
>> exit
>> elsif input.to_i.to_s == input
>> break input.to_i
>> end
>> end
>>
>> puts "You entered #{result}"
>>
>> Calling "break" with a value makes that value the return value of the
>> loop, which is what you want here.
>>
>> martin
>>
>> Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
>> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>>
>
(supressed text/html)
Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>