Ruby構造

2471 ワード

条件判断:
    

      if     then
                 
      end
     

    

      if     then
                      
      else
                       
      end
     

    

      if   1 then
          1
      elsif   2 then
          2
      else 
          3
      end             # then     
     

unless文
unless文はifとは全く逆の文です.
    

      unless    then
          
      end

      unless    then
          1
      else
          2
      end     
     

ケース文:
    

      case        
      when  1 then
          1
      when  2 then
          2
      when  3 then
          3
      else
          4
      end
     

for文:
    

      for    in       ..       do
                 
      end                        # do    

      #    for  
      for    in    do
                 
      end
     

while文:
    

      while          do
                  
      end
     

until文:
    

      until    do
                 
      end
     

timesメソッド:
    

          .times{
                
      }
     

eachメソッド:
    

        .each do |    |
                  
      end
     

loopメソッド:
終了条件はなく、ループ処理を継続するだけです.
    

      loop {
        print "Ruby"
      }
     

ループの制御:
コマンド#コマンド#
用途
break
動作を止めて、すぐにループを飛び出します
next
次のサイクルに直接ジャンプ
redo
同じ条件でこのサイクルを再開する
メソッドの定義
構文:
    

        .   (  1,   2, ... ,  n)
     

    

      def    
                   
      end
     

ライブラリをファイルに読み込む(require)
    
require "       "