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).
Ruby
First of all, familiar
hello world.
Create a program that returns this string.
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.
```ruby:hello_world.rb
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"
```
Next to the output is the input!
So, next, use the gets method to say hello to your favorite string.
```ruby:hello_name.rb
name = gets
puts "hello " + name
```
The output is
```
> ruby hello_name.rb
```
```
W
hello W
```
Now that I want to work with the CLI, I want to use ARGV [0].
```ruby:hello_name.rb
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