Ruby-syntax

提供:Dev Guides
移動先:案内検索

Ruby-構​​文

ルビで簡単なプログラムを書きましょう。 すべてのrubyファイルの拡張子は .rb になります。 そのため、次のソースコードをtest.rbファイルに配置します。

#!/usr/bin/ruby -w

puts "Hello, Ruby!";

ここでは、Rubyインタープリターが/usr/binディレクトリーで使用可能であると想定しています。 今、次のようにこのプログラムを実行してみてください-

$ ruby test.rb

これは、次の結果を生成します-

Hello, Ruby!

単純なRubyプログラムを見てきました。次に、Ruby構文に関連するいくつかの基本概念を見てみましょう。

Rubyプログラムの空白

スペースやタブなどの空白文字は、文字列に表示される場合を除き、通常Rubyコードでは無視されます。 ただし、あいまいなステートメントの解釈に使用されることもあります。 この種の解釈では、-wオプションが有効な場合に警告が生成されます。

a + b is interpreted as a+b ( Here a is a local variable)
a  +b is interpreted as a(+b) ( Here a is a method call)

Rubyプログラムの行末

Rubyは、セミコロンと改行文字をステートメントの終わりとして解釈します。 ただし、Rubyが行末に+、-、またはバックスラッシュなどの演算子を検出した場合、ステートメントの継続を示します。

Ruby識別子

識別子は、変数、定数、およびメソッドの名前です。 Ruby識別子は大文字と小文字が区別されます。 これは、RamとRAMがRubyの2つの異なる識別子であることを意味します。

Rubyの識別子名は、英数字とアンダースコア文字(_)で構成されます。

予約語

次のリストは、Rubyの予約語を示しています。 これらの予約語は、定数名または変数名として使用できません。 ただし、メソッド名として使用できます。

BEGIN do next then
END else nil true
alias elsif not undef
and end or unless
begin ensure redo until
break false rescue when
case for retry while
class if return while
def in self FILE
defined? module super LINE

Rubyのヒアドキュメント

「ヒアドキュメント」とは、複数の行から文字列を構築することを指します。 <<に続いて、文字列リテラルを終了する文字列または識別子を指定できます。現在の行から終端文字までのすべての行は、文字列の値です。

ターミネータが引用符で囲まれている場合、引用符の種類によって行指向の文字列リテラルの種類が決まります。 <<とターミネータの間にスペースがあってはならないことに注意してください。

ここに異なる例があります-

#!/usr/bin/ruby -w

print <<EOF
   This is the first way of creating
   here document ie. multiple line string.
EOF

print <<"EOF";                # same as above
   This is the second way of creating
   here document ie. multiple line string.
EOF

print <<`EOC`                 # execute commands
    echo hi there
    echo lo there
EOC

print <<"foo", <<"bar"  # you can stack them
    I said foo.
foo
    I said bar.
bar

これは、次の結果を生成します-

   This is the first way of creating
   her document ie. multiple line string.
   This is the second way of creating
   her document ie. multiple line string.
hi there
lo there
      I said foo.
      I said bar.

Ruby BEGINステートメント

構文

BEGIN {
   code
}

プログラムの実行前に呼び出される_code_を宣言します。

#!/usr/bin/ruby

puts "This is main Ruby Program"

BEGIN {
   puts "Initializing Ruby Program"
}

これは、次の結果を生成します-

Initializing Ruby Program
This is main Ruby Program

Ruby ENDステートメント

構文

END {
   code
}

プログラムの最後に呼び出される_code_を宣言します。

#!/usr/bin/ruby

puts "This is main Ruby Program"

END {
   puts "Terminating Ruby Program"
}
BEGIN {
   puts "Initializing Ruby Program"
}

これは、次の結果を生成します-

Initializing Ruby Program
This is main Ruby Program
Terminating Ruby Program

ルビーのコメント

コメントは、1行、1行の一部、または複数行をRubyインタープリターから隠します。 あなたは、行の先頭にハッシュ文字(#)を使用することができます-

# I am a comment. Just ignore me.

または、コメントはステートメントまたは式の後の同じ行にある場合があります-

name = "Madisetti" # This is again comment

次のように複数の行をコメントすることができます-

# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

別のフォームがあります。 このブロックコメントは、= begin/= endでインタープリターからの数行を隠します-

=begin
This is a comment.
This is a comment, too.
This is a comment, too.
I said that already.
=end