LoginSignup
2
0

More than 3 years have passed since last update.

ruby 演習メモ II (variable and method)

Last updated at Posted at 2020-12-31

目的

ruby のコードを実際に書いてみよう.
今回は第 II 弾.

お題 その1

受け取った引数 ARGV[0] を name という変数 (variable) に代入してそれを打ち出す code を書け.
(https://qiita.com/daddygongon/items/c51f3e0695174bb3c508 にある課題)

今回書いた code (hello_variable.rb)


name = ARGV[0]
puts "Hello #{name}."

解説

code 自体について特に言うことなし.
ちなみに, 型宣言はいらないらしい.

お題 その2

> ruby hello_method.rb Rudy
Hell Rudy.

と返す hello method を作りなさい. (先程のリンク先にある課題)

今回書いた code (hello_method.rb)


#!/usr/bin/env ruby

def hello name
  puts "Hello #{name}."
end

name = ARGV[0]
hello name

解説

method は def-end で書ける.
引数の () は省略できる.

おまけ (TDD)

先程のリンク先 (ここ) に TDD (Test Driven Development) の話がある.
ぜひ読もう.

参考資料

チャート式ruby-II(variable and method)


  • source ~/Lecture/multiscale_simulation/grad_members_20f/members/gagagagazelle/docs/c2_name_method_hello.org
2
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
2
0