Is this indentation correct? - ruby

I thought I understood the rules on indenting but almost every question I ask on S/O ends up with a comment "indent your code correctly". Just want to clear this up once and for all. This is a snippet from a project I am working on. What's indented correctly and what isn't?
def display
print " 1 2 3 4 5 6 7"
#board.each do |i|
print "\n"
#draw = "+-----+-----+-----+-----+-----+-----+-----+"
puts #draw
i.each do |n|
print "| #{n} "
end
print "|"
end
print "\n"
print #draw
end

Here is the good link to follow all the rules of the indentation
https://github.com/github/rubocop-github/blob/master/STYLEGUIDE.md

Related

No breaks in the output

Just started Ruby yesterday (for the first time). And struggling a bit. Please help.
Here's the program:
print "What's your name?"
name = gets.chomp
if name == "James"
print "Someone loves you!"
else
print "Try again #{name}!"
end
print "How old are you?"
age = gets.chomp
if age <= "25"
print "Boy, you are just a child"
elsif age >= "45"
print "Shame on you old man, craddle snacher!"
end
The output is:
enter image description here
So my concern is; why is it not beginning from a new line after "Try again Jack". I would like all the questions and answers to start at a fresh line. Please help!
PS: Just ignore the content of the program. That was just something to keep myself motivated. I don't really mean to be offensive.
2 options, print with explicit linebreaks ( \n , also works on Windows), or puts which adds a linebreak if the string does not already ends with one. These two examples result in the same output:
print "Hello\nworld\n"
puts "Hello
world"

Codecademy Ruby course: Control Flow know-how

When coding in Ruby, I came up with an error about needing to state all words the user inputed. I tried to change my code to get it to output that, but the problem remained. Here is my code and the Ruby instructions.
Instructions
Add an if/else statement inside your .each.
if the current word equals the word to be redacted, then print "REDACTED " with that extra space.
Otherwise (else), print word + " ".
The extra space in both cases prevents the words from running together.
puts text = gets.chomp
puts redact = gets.chomp
words = text.split(" ")
words = ['hi', 'hello', 'what', 'why']
words.each do |word|
if gets = words
print "Redact "
else
print word + "Incorrect"
end
end
The problem it says I have with my code is... Oops, try again. Make sure to print each word from the user's text to the console unless that word is the word to be redacted; if it is, print REDACTED (all caps!).
I would appreciate all help, please and thank you.
Ben sorry these guys aren't being too helpful. :D It's been awhile but I hope this helps. In the first section titled "What you'll be building" Codecademy gives you the exact example (the answer) to the final problem in the section. This is always true and may help in the future. What you're looking for:
puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp
words = text.split(" ")
words.each do |word|
if word != redact
print word + " "
else
print "REDACTED "
end
end
Have pass several years I know, but I wanted pass and let my solution for this excercise.
puts "Text to search through: "
text = gets.chomp
puts "Word to redact: "
redact = gets.chomp
words = text.split(',') # "," is necessary to identify each of the words
words.each do |x|
if x == redact # if words repeat, print REDACTED
print "REDACTED"
else # else, only write de word and space
print x + " "
end
end

Ruby doesn't read carriage return (\r) in output? [duplicate]

This question already has an answer here:
Ruby outputting to the same line as the previous output
(1 answer)
Closed 8 years ago.
in ruby, how do I make my output (via puts method) on one line, as opposed to line after line, and my console being flooded with output. Basically I want the output to continuously update on one line, and keep writing over the last output. I tried doing a '\r' character at the end of the string, but ruby just ignores it and keeps printing the output of my while loop line after line:
i=0
while i<90
puts "#{i} matt lao \r"
i+=1
end
I just want one continuously updated line. Thank you.
puts will always print a new line at the end, so it's doing your carriage return but then a new line after that. Use print instead.
90.times { |i| print "#{i} matt lao \r" }
To see it's actually doing the right thing, you can stick in a sleep:
90.times { |i| print "#{i} matt lao \r"; sleep 0.01 }
You could use print without any carriage return which will work just fine.
like this
2.1.1 :016 > 0.upto(4){|i| print "#{i} "}
0 1 2 3 4 => 0
puts automatically appends a new line whereas print doesn't.
You need to use print instead of puts.
For example:
90.times do |i|
print "#{i} matt lao \r"; sleep 0.02
end

Why doesn't puts() print in a single line?

This is a piece of code:
def add(a, b)
a + b;
end
print "Tell number 1 : "
number1 = gets.to_f
print "and number 2 : "
number2 = gets.to_f
puts "#{number1}+#{number2} = " , add(number1, number2) , "\n"`
When I run it, my results are spread over several lines:
C:\Users\Filip>ruby ext1.rb
Tell number 1 : 2
and number 2 : 3
3.0+3.0 =
5.0
C:\Users\Filip>
Why doesn't puts() print in a single line, and how can keep the output on one line?
gets() includes the newline. Replace it with gets.strip. (Update: You updated your code, so if you're happy working with floats, this is no longer relevant.)
puts() adds a newline for each argument that doesn't already end in a newline. Your code is equivalent to:
print "#{number1}+#{number2} = ", "\n",
add(number1, number2) , "\n",
"\n"
You can replace puts with print:
print "#{number1}+#{number2} = " , add(number1, number2) , "\n"`
or better:
puts "#{number1}+#{number2} = #{add(number1, number2)}"
Because puts prints a string followed by a newline. If you do not want newlines, use print instead.
Puts adds a newline to the end of the output. Print does not. Try print.
http://ruby-doc.org/core-2.0/IO.html#method-i-puts
You might also want to replace gets with gets.chomp.
puts "After entering something, you can see the the 'New Line': "
a = gets
print a
puts "After entering something, you can't see the the 'New Line': "
a = gets.chomp
print a

How to do a newline in output

How do I make \n actually work in my output? At the moment it just writes it all in 1 long block. Thanks for any help
Dir.chdir 'C:/Users/name/Music'
music = Dir['C:/Users/name/Music/*.{mp3, MP3}']
puts 'what would you like to call the playlist?'
#new = ''
playlist_name = gets.chomp + '.m3u'
music.each do |z|
#new += z + '\n'
end
File.open playlist_name, 'w' do |f|
f.write #new
end
Use "\n" instead of '\n'
I would like to share my experience with \n
I came to notice that "\n" works as-
puts "\n\n" // to provide 2 new lines
but not
p "\n\n"
also
puts '\n\n'
Doesn't works.
Hope will work for you!!
You can do this all in the File.open block:
Dir.chdir 'C:/Users/name/Music'
music = Dir['C:/Users/name/Music/*.{mp3, MP3}']
puts 'what would you like to call the playlist?'
playlist_name = gets.chomp + '.m3u'
File.open playlist_name, 'w' do |f|
music.each do |z|
f.puts z
end
end
Actually you don't even need the block:
Dir.chdir 'C:/Users/name/Music'
music = Dir['C:/Users/name/Music/*.{mp3, MP3}']
puts 'what would you like to call the playlist?'
playlist_name = gets.chomp + '.m3u'
File.open(playlist_name, 'w').puts(music)
For me it didn't work with adding "\n" to the end of an existing argument to puts, so as a workaround I called print on the next line with "\n" as an argument, although I suppose I could have just called puts again.
This did not produce the desired result:
puts "Coach says: #{coach_answer(user_input)}\n"
But this did:
puts "Coach says: #{coach_answer(user_input)}"
print "\n"

Resources