[RUBY] Chart type 2nd summary

!Mac OS X-10.15.7!ruby-2.7.1p83

This issue

Write the code that assigns the received argument ARGV [0] to a variable called name and issues it.

> ruby name_variable.rb Rudy
Rudy

Brief commentary

There is no need to declare a type in Ruby. Simply use equals.

name = 'Rudy'

Detailed solution

> ruby name_variable.rb Rudy

When

ruby: No such file or directory -- name_variable.rb (LoadError)

It returns that there is no such file. Start emacs

> emacs name_variable.rb

Type in the code as shown below and save as C-x → C-s.

p "Rudy"

Then, the previous error disappears and "Ruby" is displayed.

> ruby name_variable.rb Rudy
"Rudy"

Similar

Substitute it in --name and refactor it to type Rudy. --Refactor to receive ARGV [0] and return your name.

theme

> ruby hello_method.rb Rudy
Hello Rudy.

Create a hello method that returns.

Commentary

In Ruby, a group of functions and procedures defines a method. method can take zero or more arguments.

def hello(name)
  p name
end

solution

Edit hello \ _method.rb with emacs.

> emacs hello_method.rb

This time, we will consider the caller rather than the contents of the method.

hello(name)

Then, the following error message is displayed.

> ruby hello_method.rb Rudy
hello_method.rb:1:in `<main>': undefined local variable or method `name' for main:Object (NameError)

An error that means name is undefined. So, if you substitute ARGV [0] for name and execute it.

name = ARGV[0]
hello(name)

The following error message is displayed.

> ruby hello_method.rb Rudy
hello_method.rb:2:in `<main>': undefined method `hello' for main:Object (NoMethodError)

There is no Hello method. In other words

def hello(name)
    p name
end
name = ARGV[0]
hello(name)

And it is sufficient.


Recommended Posts

Chart type 2nd summary
Chart type Ruby
Chart type ruby-appendix-III (bundler)
Chart type ruby-I (puts)
Chart type ruby-appendix-IV (rake)
Chart type ruby-appendix-VI (thor, rubocop)
Chart type ruby-appendix-I (bash + emacs)
[Swift] Summary about Bool type
Summary
Java Summary of frequently searched type conversions