Chart type ruby-I (puts)

!Mac OS X-10.15.7!ruby-2.7.1p83

Intro

Purpose

Have them remember shell, emacs, and ruby at once. This is so that you can realize how useful the language ruby is in the environment of shell. Also, by knowing the uniformity of key binding between emacs and shell, we want to understand why the work efficiency of expert programmers is good.

Work procedure

The structure of this text is

It is. If you proceed according to each description, you should be able to understand the contents.

Here, please devise something to remember.

--Same configuration for easy reference ――Add a wording that explains the content in an easy-to-understand manner ――The detailed solution may be confusing, but I am conscious of test-driven. --Please include your own answers and evaluate your performance ――Please look up the contents and words you do not understand and add them to the text. ――Let's format and output the next week. Then you will have your own text.

About the content format

This text distinguishes between shell input and source code typing in emacs.

> cat hello.rb

Like this, adding command prompt (>) is intended for shell input. The output is usually plain text,

puts "Hello world."

With that feeling, it is output like ruby's source code.

puts hello_world

theme

First of all, hello \ _world.

Hello world.

I will make a program to launch.

Commentary

There are several ways to get Strings in ruby. You can use any of them, and rubyist will use all depending on the situation.

method Status of use
print Use it normally. Line break required
puts Then use it normally.Automatic line break
p When debug launches instead during coding
pp p pretty print, require 'pp'Is necessary
printf There is c. Useful when adjusting the format

There are many others, but let's remember this first.

There are two types of String, one is enclosed in single quotation marks and the other is enclosed in double quotation marks. Singles are when you don't mess with the contents, and doubles are when you mess around. We'll see this later.

Detailed solution

First, enclose it in a single

puts 'hello world'

Let's try.

> mkdir codes
> cd codes
> emacs puts_hello_world.rb

Open emacs as and type in code. Save with c-x c-s, pause with c-z,

> ruby puts_hello_world.rb

Please run as. moved?

puts 'hello world'

Similar

Please try p and print. Observe the difference, comment with # and write in p \ _print \ _hello \ _world.rb.


# start here for p and pp
p
pp

I'm getting, but I'm going with ARGV [0]!

theme

> ruby hello_name.rb Rudy

When I typed in

Hello Rudy.

Write the code that returns.

Brief commentary

If you puts, it's usually gets, but in the shell environment, which is one of the purposes, we use ARGV [0] to work with the command line interface.

puts ARGV[0]

And type in hello \ _name.rb

> ruby hello_name.rb Rudy

Please. How about? Did your dog's name come out?

puts ARGV[0]

ARGV [0] means the 0th of the argument array. On unix shell, it is normal to pass an argument such as option directly to command. For that reason, ARGV is usually prepared.

Next, think about making "Hello Rudy." There are several ways to do this. Please try this as well.

puts puts "Hello " + ARGV[0]
puts puts "Hello #{ARGV[0]}"
print print "Hello #{ARGV[0]}\n"
print print "Hello " + ARGV[0] + "\n"

For those from C, there is also printf.

printf("Hello %s.\n", ARGV[0])

But once you get used to it, it's redundant and you won't use it anymore. Perhaps it is used only when you want to align the output format of numerical calculation.

Detailed solution

First, let's make it work.

> ruby hello_name.rb Rudy
Rudy

Are you returning? Change this.

> ps

Check the state of emacs now, and start it with fg or emacs hell \ _name.rb.

puts "Hello #{ARGV[0]}"

Is often used in these situations. Are you doing it properly?

Similar

Next, consider saving "Hello Rudy." Somewhere.

ruby hello_name.rb bob > hello_name.txt

Please output a greeting to yourself in hello \ _name.txt. Ruby

Hello bob.

Or something. '>' Is a shell function that re-directs the output destination to the specified txt.

cat hello_name.txt

You can cat (con-cat-inate) the contents with.

Items learned here


Recommended Posts

Chart type ruby-I (puts)
Chart type Ruby
Chart type ruby-appendix-IV (rake)
Chart type ruby-appendix-V (rubular)
Chart type 2nd summary
Chart type ruby-appendix-VI (thor, rubocop)
Chart type ruby-appendix-I (bash + emacs)