# 配列の要素A names = ["Perl", "Python", "Ruby", "Scheme"] p names[ 0 ] p names[ 1 ] p names[ 2 ] p names[ 3 ] # 配列の要素B names = ["Perl", "Python", "Ruby", "Scheme"] names.length # 配列の要素への代入A names = ["Perl", "Python", "Ruby", "Scheme"] names[ 0 ] = "C" names[ 3 ] = "Java" p names # Ruby の配列は柔軟 abc = ["a","b","c"] abc[10] = "k" p abc # 各要素に代入する(失敗編) primes[0]=2 # 配列だということを教える@ abc = Array.new( 5 ) abc[3]=333 p abc # 配列だということを教えるB x=[] x[ 0 ] = 3 x[ 1 ] = 5 p x # 要素番号を用いて一つずつ取り出す a=[1,3,5,7,9] 5.times{ |i| print( a[ i ] , "\n" ) } a=[1,3,5,7,9] a.length.times{ |i| print( a[ i ] , "\n" ) } # coding: Windows-31J names=["Perl", "Python", "Ruby", "Scheme"] 4.times { | i | print( "#{i} 番目は #{ names[i] }\n" ) } # 要素番号を用いて一つずつ取り出す(続) # coding: Windows-31J names=["Perl", "Python", "Ruby", "Scheme"] names.length.times { | i | print( "#{i} 番目は #{ names[i] }\n" ) } # 要素番号を用いて一つずつ取り出す(続々) a=[1,3,5,7,9] (0..a.length-1).each{ |i| print( i , " " , a[ i ] , "\n" ) } a=[1,3,5,7,9] (0..a.length).each{ |i| print( i , " " , a[ i ] , "\n" ) } a=[1,3,5,7,9] (2..4).each{ |i| print( a[ i ] , "\n" ) } # 要素を直接一つずつ取り出す [1,3,5,7,9].each{ |i| print( i , "\n" ) } a=[1,3,5,7,9] a.each{ |i| print( i , "\n" ) } ["Perl", "Python", "Ruby", "Scheme"].each { | lang | print( "I like ", lang, "\n" ) } # 要素を直接一つずつ取り出す(続) names=["Perl", "Python", "Ruby", "Scheme"] names.each { | lang | print( "I like #{lang}\n" ) } # 試してみよう 3 a = [11,12,13,14,15] p a.length a.each{ |x| print( "#{x} " ) } (0..a.length-1).each{ |i| puts( "#{i}: #{a[i]} " ) } (3..a.length+1).each{ |i| puts( "#{i}: #{a[i]} " ) } n=88; [2,3,5,7,11].each{ |p| puts( "#{n} is divisible by #{p}") if n%p==0 } # 試してみよう 4 # coding: Windows-31J names=["Perl", "Python", "Ruby", "Scheme"] names[0] = "Ada" names.length.times { | i | print( "#{i} 番目は #{ names[i] }\n" ) } # 試してみよう 6 abc = ["a","b","c"] abc[1] = 111 p abc abc[3] = 3.33 p abc p abc[4] = [4,5,6] p abc # 要素と要素番号を同時に取り出す方法 # coding: Windows-31J names = ["Perl", "Python", "Ruby", "Java"] names.each_with_index{ |x,i| print( i , "番目の要素: " , x , "\n" ) } # 要素と要素番号を同時に取り出す方法 a=[1,3,5,7,9] a.each_with_index{ |x,i| print( i , " " , x , "\n" ) } # 配列の要素の参照例@ a=[4,2,1,6,7] sum = 0 a.length.times{ |i| sum += a[ i ] } print( " sum = " , sum ) a=[4,2,1,6,7] sum = 0 a.each{ |i| sum += i } print( " sum = " , sum , "\n" ) # 配列の要素の参照例A a=[4,2,1,6,7] i = 0 sum = 0 while i < a.length do sum += a[ i ] i += 1 end print( " sum = " , sum ) a=[4,2,1,6,7] i = 0 sum = 0 loop{ sum += a[ i ] i += 1 break if i == a.length } print( " sum = " , sum ) # 配列の要素の参照例B a=[4,2,1,6,7] a.length.times { |i| if i % 2 != 0 then print( a[ i ] , "\n" ) end } a=[4,2,1,6,7] a.each { |i| if i % 2 != 0 then print( i , "\n" ) end } # 配列の要素の参照例C x=[] 10.times{ |i| x[ i ] = rand( 100 ) } sum = 0 x.length.times{ |i| sum += x[ i ] } p x print( " sum = " , sum ) # 配列の要素の参照例D a=[4,2,1,6,7] a.length.times { |i| print( a[ a.length-1-i ] , "\n" ) } a=[4,2,1,6,7] (a.length-1).step(0,-1){ |i| print( a[ i ] , "\n" ) } # 配列の要素の参照例E a=[4,2,1,6,7] x=[] a.length.times { |i| x[ i ] = a[ i ] } p x a=[4,2,1,6,7] x=[] a.length.times { |i| x[ i ] = a[ i ]*a[ i ] } p x # 配列の要素の参照例E' a=[4,2,1,6,7] x=Array.new(a.length) a.length.times { |i| x[ i ] = a[ i ] } p x a=[4,2,1,6,7] x=Array.new(a.length) a.length.times { |i| x[ i ] = a[ i ]*a[ i ] } p x # 配列の要素の参照例F a=[4,2,1,6,7] a[ a.length ] = 8 p a # 配列の要素の参照例G a=[4,2,1,6,7] n=a.length n.times{ |i|   a[ n-i ] = a[ n-i-1 ] } a[ 0 ] = 5 p a # 配列の要素の参照例H a=[] n=10 n.times{ |i| a[ i ] = rand( 10 ) } a.length.times { |i| print( a[ i ] , " " , "*" * a[ i ] , "\n" ) } # 配列の要素の参照例I a=[] a[ 0 ] = 1 a[ 1 ] = 1 (2..10).each{ |i| a[ i ] = a[ i-1 ] + a[ i-2 ] } p a