4
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.

Rubyの変数と関数(メソッド)

Last updated at Posted at 2020-12-29

!macOS-11.1 !ruby-2.7.2p137

Preface (はじめに)

本記事はマルチスケールシミュレーション特論の第6回に関連する記事です.
チャート式Rubyに従って進めていきます.

今回はチャート式ruby-II(variable and method)に従ってRubyの変数とメソッドについて学んでいきます.

Ruby

variable (変数)

Rubyは変数に型宣言を必要としない. したがって, このような使い方もできる.

x = 'Ruby'
x = 19
x = false

前回の記事のようにARGV[0]も変数に代入できる.

name_variable.rb
name = ARGV[0]
puts "hello #{name}"
> ruby name_variable.rb W
-> hello W

method (メソッド)

Rubyでのfunction (関数)はmethod (メソッド)を定義することを意味する.
これはRubyはあらゆるものがオブジェクトであるかららしい.

手を使って, 覚えてみる.

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

name = ARGV[0]
hello(name)
> ruby hello_method.rb W                                                                   
-> Hello W.                                                                                  

参考資料

Ruby – Wikipedia

4
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
4
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?