5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

第6回(変数とメソッド)

Last updated at Posted at 2020-12-06

Mac OS X-10.15.7 ruby-2.7.1p83

variable

お題

受け取った引数ARGVをnameという変数(variable)に代入してそれを打ち出すcodeを書け.

> ruby name_variable.rb Rudy
Rudy

簡単な解説

rubyの変数は型宣言の必要性がない.イコールで代入.

name = 'Rudy'

詳しい解法

とりあえずRubyを出力する.name_variable.rbに以下のように書く.

p "Rudy"

結果

> ruby name_variable.rb Rudy
"Rudy"

nameに代入してRudyと打ち出すように変更

name = "Ruby"
p name

結果

> ruby name_variable.rb Rudy
"Rudy"

ARGVを受け取って,自分の名前を返すように変更

name = ARGV[0]
p name

結果

> ruby name_variable.rb Rudy
"Rudy"

method

お題

> ruby hello_method.rb Rudy
Hell Rudy.

と返すhello methodを作りなさい.

解説

methodは0個以上の引数(argument)を取ることができる.例えば次の通り.

def hello(name)
  p name
end

解法

def hello(name)
    puts "Hello #{name}."
end
name = ARGV[0]
hello(name)

結果

> ruby hello_method.rb Rudy
Hell Rudy.

Footnotes:

1

DEFINITION NOT FOUND.


  • source ~/grad_members_20f/members/yoshida/c2_name_method_hello.org
5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?