!Mac OS X-10.15.7!ruby-2.7.1p83
Write the code that assigns the received argument ARGV [0] to a variable called name and issues it.
> ruby name_variable.rb Rudy
Rudy
There is no need to declare a type in Ruby. Simply use equals.
name = 'Rudy'
> 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"
Substitute it in --name and refactor it to type Rudy. --Refactor to receive ARGV [0] and return your name.
> ruby hello_method.rb Rudy
Hello Rudy.
Create a hello method that returns.
In Ruby, a group of functions and procedures defines a method. method can take zero or more arguments.
def hello(name)
p name
end
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