LoginSignup
1
0

More than 3 years have passed since last update.

第六回講義メモ

Last updated at Posted at 2020-12-31

!Mac OS X-10.15.7 !ruby-2.6.3p62

Variable, Method

第6回目では変数とメソッドについてまとめる.

例題1

受け取った引数を, nameという変数に代入し, それを吐き出すcodeを書くこと.(rubyでは引数に, ARGV[0]を用いる.)

> ruby name_variable.rb ruby
ruby

rubyではpythonと同様に型宣言の必要がなく, イコールで代入される.

name = 'ruby'

例題2

受け取った引数にhelloをつけて返すmethodを作ること.

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

helloというメソッドを作成し, 引数を代入している.

結果

> ruby hello_method.rb Rudy
Hello Rudy.

メソッド補足

Rubyではfunctionやprocedureなどのまとまりはmethodを定義(definition)することができる.methodは0個以上の引数(argument)を取ることが可能.(以下例)

def hello(name)
  p name
end

参考サイト


  • source ~/my_ruby/grad_members_20f/members/drop-sd/lectures/no6.org
1
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
1
0