# 配列の要素の参照方法@ a=[1,3,5,7,9] a.length.times{ |i| print( a[ i ] , "\n" ) } 5.times{ |i| print( a[ i ] , "\n" ) } (0..a.length-1).each{ |i| print( a[ i ] , "\n" ) } # 配列の要素の参照方法A a=[1,3,5,7,9] a.each{ |i| print( i , "\n" ) } [1,3,5,7,9].each{ |i| print( i , "\n" ) } # 配列の要素への代入 name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] name[ 3 ] = "d" test[ 3 ] = 90 p name p test # 最後の要素への追加 name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] name[ name.length ] = "F" test[ test.length ] = 70 p name p test # 平均点を求める@ name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] sum = 0 test.length.times{ |i| sum += test[ i ] } print( "Average --> " , sum / test.length ) # 平均点を求めるA name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] sum = 0 (0..test.length-1).each{ |i| sum += test[ i ] } print( "Average --> " , sum / test.length ) # 平均点を求めるB name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] sum = 0 test.each{ |i| sum += i } print( "Average --> " , sum / test.length ) # 平均点未満の名前を出力 # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] sum = 0 test.each{ |i| sum += i } average = sum / test.length print( "平均点未満は...\n" ) test.length.times{ |i| if average > test[ i ] then print( name[ i ] , ": " , test[ i ] , "点\n" ) end } # 最高点とその名前を出力 # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] max = 0 test.length.times{ |i| if test[ max ] < test[ i ] then max = i end } print( "最高点は" , name[ max ] , " の " , test[ max ] , "点です\n" ) # 最低点とその名前を出力 # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] min = 0 test.length.times{ |i| if test[ min ] > test[ i ] then min = i end } print( "最低点は" , name[ min ] , " の " , test[ min ] , "点です\n" ) # 検索@ # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] search = "B" name.length.times{ |i| if name[ i ] == search then print( search , " の点は " , test[ i ] , "点です\n" ) break end } # 検索@(書き方に注意!) # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] search = "B" name.length.times{ |i| if name[ i ] == search then break end } print( search , " の点は " , test[ i ] , "点です\n" ) # ローカル変数@ 10.times{ a = 10 } print( a , "\n" ) # ローカル変数A a = 0 10.times{ a = 10 } print( a , "\n" ) # グローバル変数 10.times{ $a = 10 } print( $a , "\n" ) # 検索@(書き方に注意!)(続) # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] i=0 search = "B" name.length.times{ |i| if name[ i ] == search then break end } print( search , " の点は " , test[ i ] , "点です\n" ) # 検索@(書き方に注意!)(続) x = 0 print( " x = " , x , "\n" ) 10.times{ |x| print( " loop --> " , x , "\n" ) } print( " x = " , x , "\n" ) # 検索@(結論としては…) # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] result=0 search = "B" name.length.times{ |i| if name[ i ] == search then result = i break end } print( search , " の点は " , test[ result ] , "点です\n" ) # 配列の初期化@ sieve = Array.new(10).fill{ 1 } p sieve # 配列の初期化A sieve = Array.new(10).fill{ |i| i } p sieve # コマンドライン引数C print( ARGV[ 0 ] , "\n" ) print( ARGV[ 1 ] , "\n" ) print( ARGV[ 2 ] , "\n" ) # コマンドライン引数:プログラム print( ARGV[0] + " + " + ARGV[1]+ " = " ) print( ARGV[0].to_i + ARGV[1].to_i ) print( "#{ARGV[0]} + #{ARGV[1]} = " ) print( ARGV[0].to_i + ARGV[1].to_i ) # 浮動小数点数にすると print( ARGV[0] + " + " + ARGV[1]+ " = " ) print( ARGV[0].to_f + ARGV[1].to_f ) # 3個にすると print( ARGV[0] + " + " + ARGV[1] + " + " + ARGV[2] + " = " ) print( ARGV[0].to_f + ARGV[1].to_f + ARGV[2].to_f ) # 検索A # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] search = ARGV[ 0 ] name.length.times{ |i| if name[ i ] == search then print( search , " の点は " , test[ i ] , "点です\n" ) break end } # 検索B # coding: Windows-31J name = [ "A" , "B" , "C" , "D" , "E" ] test = [ 85 , 60 , 5, 100 , 50 ] line = ARGV[ 0 ] val = line.to_i name.length.times{ |i| if test[ i ] == val then print( val , " 点は " , name[ i ] , "です\n" ) break end } # 二重ループ (0..9).each{ |x| (0..9).each{ |y| z = x*x + y*y print( " x = " , x , " y = " , y , ": z= " , z , "\n" ) } } 10.times{ |x| 10.times{ |y| z = x*x + y*y print( " x = " , x , " y = " , y , ": z= " , z , "\n" ) } } x = 0 while x < 10 do y= 0 while y < 10 do z = x * x + y * y print( " x = " , x , " y = " , y , ": z= " , z , "\n" ) y += 1 end x += 1 end # 二重ループの例@ # coding: Windows-31J (1..9).each{ |x| (1..9).each{ |y| printf( " %d×%d=%2d" , x , y , x * y ) } print( "\n" ) } # 二重ループの例A (1..9).each{ |x| (1..9).each{ |y| if x == y then print( "1 " ) else print( "0 " ) end } print( "\n" ) } # 二重ループの例B (1..9).each{ |x| (1..9).each{ |y| if x == (10-y) then print( "1 " ) else print( "0 " ) end } print( "\n" ) } # 二重ループの例C (1..9).each { |i| (1..i).each { |j| print( j )  }  print ( "\n" ) } # 二重ループの例D (1..9).each { |i| (1..(10-i)).each{ |j| print( j ) } print ( "\n" ) } # 二重ループの例E 11.times { |i| d = Math.sqrt( 100 - i*i ).to_i (1..d).each{ print( " " ) } ((d+1)..10).each{ print( "*" ) } print( "\n" ) } # 二重ループの例F 11.times { |i| d = Math.sqrt( 400 - 4*i*i ).to_i (1..d).each{ print( " " ) } ((d+1)..20).each{ print( "*" ) } print( "\n" ) }