From terminal to Ruby (standard input / output)


Preface (Introduction)

This article is related to Part 5 of the Special Lecture on Multiscale Simulation. We will proceed according to Chart Ruby.

This time, we will learn the standard input / output of Ruby according to Chart type ruby-I (puts).


Standard input / output

First of all, familiar

hello world.

Create a program that returns this string.

  1. String output

There are multiple functions for outputting strings in Ruby.

Typical ones

 | method |Explanation|new line|
 | print  |Argument object is converted to a character string and output to standard output.                                        | ×  |
 | puts   |Convert the argument object to a character string, add a line break, and output to standard output..                            | ○  |
 | p      |puts for debug,Raw strings are output to standard output without being processed even if they are escaped..| ○  |
 | pp     |p pretty print,Standard output is easier to understand than p. require'pp'Is necessary.               | ○  |

Let's make it now.

print "hello world1"
puts "hello world2"
p "hello world3"
pp "hello world4"

The output is

> ruby hello_world.rb

hello world1hello world2
"hello world3"
"hello world4" 
  1. Enter a character string

Next to the output is the input!

So, next, use the gets method to say hello to your favorite string.

name = gets
puts "hello " + name

The output is

> ruby hello_name.rb

hello W

Now that I want to work with the CLI, I want to use ARGV [0].

puts "hello #{ARGV[0]}"

The output is

> ruby hello_name.rb W
-> hello W


Try writing standard output to a suitable file using Redirect>.

> ruby hello_name.rb Woo > hello_name.txt

Check if it was output properly

> cat hello_name.txt
-> hello Woo

Recommended Posts

From terminal to Ruby (standard input / output)
[ruby] How to receive values from standard input?
Ruby input / output
Ruby standard output
Ruby standard input
[Java] Color the standard output to the terminal
From Java to Ruby !!
[Java] How to get and output standard input
[Ruby] How to use standard output in conditional branching
How to output standard from an array with forEach
[Ruby] Receive input from console
Ruby input / output
Ruby standard input
Ruby Learning # 10 Getting User Input
[Ruby] Receive input from console
[Rails] Get standard input for multi-line data
From terminal to Ruby (standard input / output)
[Ruby] Summary of how to get values from standard input [Paiza skill check measures]
[Java] I want to test standard input & standard output with JUnit
Introduction to Ruby (from other languages)
Learning Ruby with AtCoder 11 How to receive frequently used standard input
I tried to build Ruby 3.0.0 from source
Learning Ruby with AtCoder 12 How to use standard output properly (p / puts / print)
[ruby] drill output
[ruby] drill output
[ruby] drill output
[ruby] drill output
Output the maximum value from the array using Java standard output
How to input / output IBM mainframe files in Java?
[Updated from time to time] Ruby on Rails Convenient methods
Ruby Regular Expression Extracts from a specific string to a string
paiza skill check standard input / output summary [Java edition]
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Migrate from JUnit 4 to JUnit 5
Ruby calorie calculation output
Output triangle in Ruby
Output embedded Tomcat access log to standard output with Spring Boot
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
From Ruby on Rails error message display to Japanese localization
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
How to pick up the input value by asynchronous communication and output the desired data from the server
[Output] Learn Ruby from what you want to do at the fastest speed-Part 2 "Create a flowchart"