Why doesn't puts() print in a single line?
(Update: You updated your code, so if you're happy working with floats, this is no longer relevant.)gets()
includes the newline. Replace it with gets.strip
.
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)}"
Why does puts 'foo' and puts foo\n result in the same output in Ruby?
After reading Cary Swoveland's comment I've realized that it is not at all obvious how puts
works, because its documentation is quite scarce:
puts(obj, ...) → nil
Equivalent to
$stdout.puts(obj, ...)
It doesn't even bother to explain what $stdout
is, nor does it provide a link.
$stdout
is one of Ruby's pre-defined global variables. It refers to the standard output which in Ruby happens to be an instance of IO
:
$stdout
#=> #<IO:<STDOUT>>
So "Equivalent to $stdout.puts(obj, ...)
" means that we have to read the documentation for IO#puts
:
Writes the given object(s) to ios. Writes a newline after any that do not already end with a newline sequence. Returns
nil
.
There you go: puts
only adds a newline if the object does not already ends with one.
To get the expected multi-line output, you can simply call puts
for each line:
puts 'abc'
or
puts 'abc'
puts
or
puts 'abc'
puts
puts
Why strings are not coming in same line in ruby
Kernel#gets
gives you a string with the \n
added to the end of the string. That causing the output in the multiple lines.
To make your output as you wanted it to be, you need to use #chomp
method, like gets.chomp
.
Code output in single line?
Use the #chomp
method
name = gets.chomp # instead of name = gets
age = gets.chomp # instead of age = gets
I rewrite the code :
print "Please enter your name: "
name = gets.chomp
print "Please enter your age: "
age = gets.chomp
print "Entered details are: ",name, " ", age, "\n"
and ran it :
[arup@Ruby]$ ruby a.rb
Please enter your name: arup
Please enter your age: 27
Entered details are: arup 27
[arup@Ruby]$
#chomp
Returns a new String with the given record separator removed from the end of str (if present). If $/ has not been changed from the default Ruby record separator, then chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n).
#gets
Returns (and assigns to
$_
) the next line from the list of files in ARGV (or $*), or from standard input if no files are present on the command line.
C output on terminal doesn't have it's own line
Use a newline character, represented as '\n'
. The slash is an escape character for a few special characters, such as tab '\t'
, and carriage return '\r'
.
printf("Hello World\n");
printf doesn't print a string immediately
The output stream stdout
is buffered by default, so if you want immediate output you'll need to flush the output stream - using fflush
- or cause a newline to be printed in the printf
:
printf("Starting nets allocation...");
fflush(stdout);
Or:
printf("Starting nets allocation...\n");
Note that you can also control buffering at a file pointer level using the setbuf
function from stdio.h:
setbuf(stdout, NULL);
The second argument to setbuf
is a buffer supplied by the caller to be used for buffering output to the stream. Passing NULL indicates that buffering is to be disabled, and is equivalent to:
setvbuf(stdout, NULL, _IONBF, 0);
which also disables buffering on the specified stream.
See docs for setbuf
here.
How do you print the final result in a single line?
You can save the fifth words in a list during the loop and join them after it. Below I show some modifications in your code to achieve this:
file = input('Chapter file: ')
with open(file, 'r') as chapter:
final = chapter.read()
x = final.split()
combined = []
for i in range(0, 200, 5):
combined.append(x[i])
print(' '.join(combined))
Related Topics
Jekyll:New Posts Not Being Generated
Nameerror: Undefined - Have Parsing Rules for Local Variables Changed in Ruby 2.1.2
Static Variables in Ruby, Like in C Functions
Heroku: Error Pushing Rails App to Heroku, Heroku Can't Find Rails App
Gems Not in Local Gems After Bundle Install
What's a Semantically-Correct Way to Parse CSV from SQL Server 2008
Rails 5.2 Activestorage Save and Then Read Exif Data
My Class Name Conflicting with Ruby'S
Ruby Tcpserver to Get Client Ip Address
Find Out Which Words in a Large List Occur in a Small String
How to Collapse Double Splat Arguments into Nothing
Using the Right Exception Subclass in Ruby
Trying to Find Vowels of a String Using Ruby While Loops
How to Build a Docker Image for a Ruby Project Without Build Tools
Why Non-Explicit Splat Param Plus Default Param Is Wrong Syntax for Method Definition in Ruby 1.9