LoginSignup
12
0

More than 3 years have passed since last update.

【Week 5】bash, ruby_first

Last updated at Posted at 2020-11-25

はじめに

マルチスケールシミュレーション特論の講義メモです.講義メモのインデックスはコチラ

今回の参考資料はチャート式ruby-appendix-I(bash + emacs)チャート式ruby-I(puts)です.

概要

今回から Ruby の講義に入っていく.講義のメモは各回

  • チャート式 Ruby
  • Ruby 開発周辺情報

の2点を軸としてまとめる.

チャート式 Ruby 一覧

チャート式 Rubyで扱う内容は以下を予定している.

  1. puts (出力)
  2. variable and method (変数とメソッド)
  3. if-else, case, array and each (条件分岐と配列まわり)
  4. gem (ライブラリの使い方)
  5. recursion (再帰)
  6. class
  7. 仕上げ問題

Ruby 開発周辺情報 一覧

Ruby 開発周辺情報で扱う内容は以下を予定している.

  1. bash + emacs
  2. myhelp
  3. bundler
  4. rake
  5. rubular
  6. thor
  7. rubcoop

チャート式 Ruby

参照記事はコチラ

puts

今回から Ruby の講義に入る.はじめに学ぶ内容はプログラミングの初歩中の初歩である文字列の出力についてである.まずはみんな大好き

Hello World.

を出力するプログラムを作ってみる.

プログラムを管理するためにcodes ディレクトリを作成し, puts_hello_world.rb を編集する.

> mkdir codes
> cd codes
> emacs puts_hello_world.rb

puts_hello_world.rb は以下のとおり.

puts "hello world."

これを実行してみると,

> ruby puts_hello_world.rb
hello world.

期待通り hello world. が出力された.

Ruby で文字を出力できるメソッドは puts だけでない.以下にいくつか例を挙げる.

メソッド 使い方
print 引数の値を出力. 改行なし
puts 引数の値を出力.改行あり
p デバッグ用の出力.引数のオブジェクトがわかる
pp require 'pp'が必要.p と同じデバッグ用
printf c 言語と同等.複数の引数を渡すことができる

上記のメソッドを比較するプログラム p_print_hello_world.rb を作成する.p_print_hello_world.rb は以下のとおり.

puts "hello world."
print "hello world."
p "hello world."
pp "hello world."
printf "hello world."

これを実行してみると,

> ruby p_print_hello_world.rb 
hello world.
hello world."hello world."
"hello world."
hello world

各々出力が異なるので考察してみる.

> puts "hello world."
hello world. 
# 

> print "hello world."
hello world. (改行なし)
#

> p "hello world."
"hello world."
# 

> pp "hello world."
"hello world."
# 

> printf "hello world."
hello world
#

ARGV[0]

続いてコマンドライン引数を用いて

> ruby hello_name.rb Rudy

と実行したときに,

Hello Rudy.

と出力するプログラムを作ってみる.

Ruby でコマンドライン引数を扱うには ARGV[]を使う.python でも sys.argv (sys モジュールの argv メソッド) を使うので,コマンドライン引数を使いたいときはどの言語も argv で検索すれば良さそう.とりあえず hello_name.py を作成し,

puts ARGV[0]

を実行してみる.

> ruby hello_name.rb Rudy
Rudy

確かにコマンドラインの値を出力することができた.

ARG[0]は引数 (argment) 配列のインデックス番号 0 を指している.unix shell 上では command の option として引数を直接渡せるように用意されているらしい.

次にコマンドライン引数と文字列を組み合わせて出力させたい.Ruby ではいくつか方法があるので以下にまとめる.

メソッド プログラム
puts puts "Hello " + ARGV[0]
puts puts "Hello #{ARGV[0]}"
print print "Hello #{ARGV[0]}\n"
print print "Hello " + ARGV[0] + "\n"

先に書いたように printf でも問題なく出力できる.Ruby on Rails で Web アプリケーションを作った際に一番お世話になった2番目のプログラムで書いたものを実行する.

> ruby hello_name.rb Rudy
Hello Rudy.

無事コマンドライン引数を用いて出力をするプログラムを作成できた.

リダイレクト

実行結果を別ファイルに保存するには,以下のようにリダイレクトを用いるとよい.

> ruby hello_name.rb Hiroki > hello_name.txt

'>' はリダイレクト(redirect)といい,出力を txt に変更したものが指定したファイル先に保存される.

Hello Hiroki.

また,'<' の場合は 指定したファイルを入力として与えることができる.ファイルの中身は cat (concatinate)

cat hello_name.txt

で指定したファイルの中身を見ることができる.

Ruby 開発周辺情報

参照記事はコチラ

bash + emacs

shell

  • command 関連
    • command [objects]
    • command [options] [objects]
  • directory 関連
    • open [file] # ファイルを開く
    • mkdir [DIR] # ディレクトリの作成 (make directory)
    • pwd # カレントディレクトリ(print working directory)
    • cd [DIR] # ディレクトリの移動 (change directory)
    • cd .. # 一つ上のディレクトリへ移動
    • ls -al # ファイルやディレクトリの表示 (list all, long)
  • process 関連
    • ps, top # process status
    • fg, bg # fore, back ground
    • kill -9 PID # kill process id

などのコマンドで操作するCLI.(command line interface)

emacs

key-bind が充実しているテキストエディタ.というよりも key-bind での操作が前提のテキストエディタといった方が適している.emacs による一連の編集動作は以下のとおり.

> emacs hoge.org    # emacsの起動

# ファイル編集後
c-x c-s    # ファイルのセーブ

c-x c-c (quit) or c-z (stop)    # emacsの終了 

quit は emacs を終了,stop はプロセスを残して一時終了する.stop の場合は

> fg

で再び編集中の emacs を呼び出すことができる.

key-bind について,より詳しい内容は以下の記事に随時追加していきます.

【Memo】Emacs key-bind (Editing)

git

git について,詳しい内容は以下の記事に随時追加していきます.

【Memo】github & Qiita

次回の講義内容 <2020-10-28 Wed>

次回の講義は

だそうです.


  • source ~/grad_members_20f/members/e79a93e5b7b1/posts/class/c5_20201021.org
12
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
12
0