LoginSignup
6
0

More than 3 years have passed since last update.

マルチスケールシミュレーション特論:第7回

Last updated at Posted at 2020-12-30

shebang

shebang(シバン,シェバン)とは,スクリプトの#!から始まる1行目のこと.スクリプトを読み込むインタプリタを指定できる.例えば,以下のようなファイル helloを書く.

hello
#!/usr/bin/env ruby
p 'hello world'

chmod a+x helloとし,executableにする.すると./helloとするだけで実行できるようになる.

pathの設定

exportコマンドを使って環境変数を設定できる.例えば~/binをpathに追加する場合は~/.bashrcに以下の一行を追記する

export PATH=".:~/bin:$PATH"

これで次回ログイン以降は~/binがpathに追加されるが,ログアウトせずに今すぐ変更を反映したい場合は

source ~/.bashrc

と,sourceコマンドを使えばできる.

Ruby勉強:第3回

条件分岐ifの使い方

うるう年ならtrueを返し,そうでないならfalseを返すメソッド leap?を実装する場合,以下のように書ける.

def leap?(year)
  if year % 4 != 0
    return false
  elsif year % 100 != 0
    return true
  elsif year % 400 == 0
    return true
  else 
    return false
  end
end

array, eachの使い方

array, eachを使ってメソッド leap?をテストするコードは以下のように書ける.

[2004, 1999, 1900, 2000].each do |year|
  p leap?(year)
end

参考文献


  • source ~/grad_members_20f/members/ryuta-kikuchi/qiita_articles/lecture7.org
6
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
6
0