다음은 Python용 소스파일 testForFor.py를 Ruby용으로 수정한 것이다.
Python 언어에서의 print 문과 달리 Ruby 언어의 print 문은 개행(newline) 문자를 포함하여지 않는다.

Python 언어의에서 쓰이는 조건 분기 구문

        if 조건식1:
            블럭1
        elif 조건식2:
            블럭2
        elif 조건식3:
            블럭3
        else:
            블럭4

에 해딩하는 Ruby 언어의 구문은

        if 조건식1 then
            블럭1
        elsif 조건식2 then
            블럭2
        elsif 조건식3 then
            블럭3
        else
            블럭4
        end

이다.



#  Filename: testForFor.rb
#
#  Execute:  ruby testForFor.rb
#        Or  jruby testForFor.rb
#
#  Date:  2008. 3. 3.

def getDan(dan)
    t = [""]*19
    for j in 0...19
 sa = "%s" % dan
 if sa.size < 2 then
            sa = " %s" % dan
        end
 sb = "%s" % (j + 1)
 if sb.size < 2 then
     sb = " %s" % (j + 1)
        end
 sval = "%s" % (dan*(j + 1))
 if sval.size < 2 then
     sval = "  %s" % (dan*(j + 1))
 elsif sval.size < 3 then
     sval = " %s" % (dan*(j + 1))
        end
        t[j] = sa + " x " + sb + " = " + sval
    end
    return t
end

# 19단표를 모두 80컬럼 컨솔에 출력한다.
def printAllNineteenDan()
    arr = [[""]*18]*19
    for i in 2...20
 arr[i - 2] = getDan(i)
    end
   
    # Ruby 소스 코드에서도 배열 대신 리스트를 사용한다.
    d = [ 2, 7, 11, 16 ]      # 각 줄단위 블럭의 첫단
    counter = [ 5, 4, 5, 4 ]  # 각 줄단위 블럭에 속한 단의 개수
    lines = [""]*19

    for k in 0...d.size
        # 8-바이트 길이의 한 줄씩 완성
        for i in 0...19 do
            lines[i] = arr[d[k]-2][i]
            for j in 1...counter[k]
                lines[i] += "   " +  arr[d[k]-2+j][i]
            end
        end
       
        # 80 바이트 길이의 한 줄씩 출력
        for i in 0...19 do
            print lines[i] + "\n"
        end
        print "\n"
    end
end

printAllNineteenDan()




실행> ruby testForFor.rb 
또는   jruby testForFor.rb

 2 x  1 =   2    3 x  1 =   3    4 x  1 =   4    5 x  1 =   5    6 x  1 =   6
 2 x  2 =   4    3 x  2 =   6    4 x  2 =   8    5 x  2 =  10    6 x  2 =  12
 2 x  3 =   6    3 x  3 =   9    4 x  3 =  12    5 x  3 =  15    6 x  3 =  18
 2 x  4 =   8    3 x  4 =  12    4 x  4 =  16    5 x  4 =  20    6 x  4 =  24
 2 x  5 =  10    3 x  5 =  15    4 x  5 =  20    5 x  5 =  25    6 x  5 =  30
 2 x  6 =  12    3 x  6 =  18    4 x  6 =  24    5 x  6 =  30    6 x  6 =  36
 2 x  7 =  14    3 x  7 =  21    4 x  7 =  28    5 x  7 =  35    6 x  7 =  42
 2 x  8 =  16    3 x  8 =  24    4 x  8 =  32    5 x  8 =  40    6 x  8 =  48
 2 x  9 =  18    3 x  9 =  27    4 x  9 =  36    5 x  9 =  45    6 x  9 =  54
 2 x 10 =  20    3 x 10 =  30    4 x 10 =  40    5 x 10 =  50    6 x 10 =  60
 2 x 11 =  22    3 x 11 =  33    4 x 11 =  44    5 x 11 =  55    6 x 11 =  66
 2 x 12 =  24    3 x 12 =  36    4 x 12 =  48    5 x 12 =  60    6 x 12 =  72
 2 x 13 =  26    3 x 13 =  39    4 x 13 =  52    5 x 13 =  65    6 x 13 =  78
 2 x 14 =  28    3 x 14 =  42    4 x 14 =  56    5 x 14 =  70    6 x 14 =  84
 2 x 15 =  30    3 x 15 =  45    4 x 15 =  60    5 x 15 =  75    6 x 15 =  90
 2 x 16 =  32    3 x 16 =  48    4 x 16 =  64    5 x 16 =  80    6 x 16 =  96
 2 x 17 =  34    3 x 17 =  51    4 x 17 =  68    5 x 17 =  85    6 x 17 = 102
 2 x 18 =  36    3 x 18 =  54    4 x 18 =  72    5 x 18 =  90    6 x 18 = 108
 2 x 19 =  38    3 x 19 =  57    4 x 19 =  76    5 x 19 =  95    6 x 19 = 114

 7 x  1 =   7    8 x  1 =   8    9 x  1 =   9   10 x  1 =  10
 7 x  2 =  14    8 x  2 =  16    9 x  2 =  18   10 x  2 =  20
 7 x  3 =  21    8 x  3 =  24    9 x  3 =  27   10 x  3 =  30
 7 x  4 =  28    8 x  4 =  32    9 x  4 =  36   10 x  4 =  40
 7 x  5 =  35    8 x  5 =  40    9 x  5 =  45   10 x  5 =  50
 7 x  6 =  42    8 x  6 =  48    9 x  6 =  54   10 x  6 =  60
 7 x  7 =  49    8 x  7 =  56    9 x  7 =  63   10 x  7 =  70
 7 x  8 =  56    8 x  8 =  64    9 x  8 =  72   10 x  8 =  80
 7 x  9 =  63    8 x  9 =  72    9 x  9 =  81   10 x  9 =  90
 7 x 10 =  70    8 x 10 =  80    9 x 10 =  90   10 x 10 = 100
 7 x 11 =  77    8 x 11 =  88    9 x 11 =  99   10 x 11 = 110
 7 x 12 =  84    8 x 12 =  96    9 x 12 = 108   10 x 12 = 120
 7 x 13 =  91    8 x 13 = 104    9 x 13 = 117   10 x 13 = 130
 7 x 14 =  98    8 x 14 = 112    9 x 14 = 126   10 x 14 = 140
 7 x 15 = 105    8 x 15 = 120    9 x 15 = 135   10 x 15 = 150
 7 x 16 = 112    8 x 16 = 128    9 x 16 = 144   10 x 16 = 160
 7 x 17 = 119    8 x 17 = 136    9 x 17 = 153   10 x 17 = 170
 7 x 18 = 126    8 x 18 = 144    9 x 18 = 162   10 x 18 = 180
 7 x 19 = 133    8 x 19 = 152    9 x 19 = 171   10 x 19 = 190

11 x  1 =  11   12 x  1 =  12   13 x  1 =  13   14 x  1 =  14   15 x  1 =  15
11 x  2 =  22   12 x  2 =  24   13 x  2 =  26   14 x  2 =  28   15 x  2 =  30
11 x  3 =  33   12 x  3 =  36   13 x  3 =  39   14 x  3 =  42   15 x  3 =  45
11 x  4 =  44   12 x  4 =  48   13 x  4 =  52   14 x  4 =  56   15 x  4 =  60
11 x  5 =  55   12 x  5 =  60   13 x  5 =  65   14 x  5 =  70   15 x  5 =  75
11 x  6 =  66   12 x  6 =  72   13 x  6 =  78   14 x  6 =  84   15 x  6 =  90
11 x  7 =  77   12 x  7 =  84   13 x  7 =  91   14 x  7 =  98   15 x  7 = 105
11 x  8 =  88   12 x  8 =  96   13 x  8 = 104   14 x  8 = 112   15 x  8 = 120
11 x  9 =  99   12 x  9 = 108   13 x  9 = 117   14 x  9 = 126   15 x  9 = 135
11 x 10 = 110   12 x 10 = 120   13 x 10 = 130   14 x 10 = 140   15 x 10 = 150
11 x 11 = 121   12 x 11 = 132   13 x 11 = 143   14 x 11 = 154   15 x 11 = 165
11 x 12 = 132   12 x 12 = 144   13 x 12 = 156   14 x 12 = 168   15 x 12 = 180
11 x 13 = 143   12 x 13 = 156   13 x 13 = 169   14 x 13 = 182   15 x 13 = 195
11 x 14 = 154   12 x 14 = 168   13 x 14 = 182   14 x 14 = 196   15 x 14 = 210
11 x 15 = 165   12 x 15 = 180   13 x 15 = 195   14 x 15 = 210   15 x 15 = 225
11 x 16 = 176   12 x 16 = 192   13 x 16 = 208   14 x 16 = 224   15 x 16 = 240
11 x 17 = 187   12 x 17 = 204   13 x 17 = 221   14 x 17 = 238   15 x 17 = 255
11 x 18 = 198   12 x 18 = 216   13 x 18 = 234   14 x 18 = 252   15 x 18 = 270
11 x 19 = 209   12 x 19 = 228   13 x 19 = 247   14 x 19 = 266   15 x 19 = 285

16 x  1 =  16   17 x  1 =  17   18 x  1 =  18   19 x  1 =  19
16 x  2 =  32   17 x  2 =  34   18 x  2 =  36   19 x  2 =  38
16 x  3 =  48   17 x  3 =  51   18 x  3 =  54   19 x  3 =  57
16 x  4 =  64   17 x  4 =  68   18 x  4 =  72   19 x  4 =  76
16 x  5 =  80   17 x  5 =  85   18 x  5 =  90   19 x  5 =  95
16 x  6 =  96   17 x  6 = 102   18 x  6 = 108   19 x  6 = 114
16 x  7 = 112   17 x  7 = 119   18 x  7 = 126   19 x  7 = 133
16 x  8 = 128   17 x  8 = 136   18 x  8 = 144   19 x  8 = 152
16 x  9 = 144   17 x  9 = 153   18 x  9 = 162   19 x  9 = 171
16 x 10 = 160   17 x 10 = 170   18 x 10 = 180   19 x 10 = 190
16 x 11 = 176   17 x 11 = 187   18 x 11 = 198   19 x 11 = 209
16 x 12 = 192   17 x 12 = 204   18 x 12 = 216   19 x 12 = 228
16 x 13 = 208   17 x 13 = 221   18 x 13 = 234   19 x 13 = 247
16 x 14 = 224   17 x 14 = 238   18 x 14 = 252   19 x 14 = 266
16 x 15 = 240   17 x 15 = 255   18 x 15 = 270   19 x 15 = 285
16 x 16 = 256   17 x 16 = 272   18 x 16 = 288   19 x 16 = 304
16 x 17 = 272   17 x 17 = 289   18 x 17 = 306   19 x 17 = 323
16 x 18 = 288   17 x 18 = 306   18 x 18 = 324   19 x 18 = 342
16 x 19 = 304   17 x 19 = 323   18 x 19 = 342   19 x 19 = 361


Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
Posted by Scripter
,

소스 파일명: testWhile.rb

  1. #
  2. #  Filename: testWhile.rb
  3. #
  4. #  Purpose:  Example using the while loop syntax
  5. #                while ....
  6. #
  7. # Execute: ruby testWhile.rb -200 300
  8. #
  9. # 사용법 표시
  10. def printUsage()
  11.     print "Using: ruby testWhile.rb [integer1] [integer2]\n"
  12.     print "This finds the greatest common divisor of the given two integers.\n"
  13. end
  14. if (ARGV.length != 2)
  15.     printUsage()
  16.     exit(1)
  17. end
  18. # --------------------------------------
  19. # 명령행 인자의 두 스트링을 가져와서
  20. # 정수 타입으로 변환하여
  21. # 변수 val1과 val2에 저장한다.
  22. val1 = ARGV[0].to_i
  23. val2 = ARGV[1].to_i
  24. # a는 |val1|, |val2| 중 큰 값
  25. a = val1.abs
  26. b = val2.abs
  27. if (a < b) then
  28.     a = val2.abs
  29.     b = val1.abs
  30. end
  31. if (b == 0)
  32.     print "GCD(#{val1}, #{val2}) = #{a}\n"
  33.     exit(0)
  34. end
  35. # --------------------------------------
  36. # Euclidean 알고리즘의 시작
  37. #
  38. # a를 b로 나누어 몫은 q에, 나머지는 r에 저장
  39. q = a / b
  40. r = a % b
  41. # --------------------------------------
  42. # Euclidean 알고리즘의 반복 (나머지 r이 0이 될 때 까지)
  43. while (r != 0)
  44.     a = b
  45.     b = r
  46.     q = a / b
  47.     r = a % b
  48. end
  49. # 나머지가 0이면 그 때 나눈 수(제수) b가 최대공약수(GCD)이다.
  50. gcd = b
  51. # 최대공약수(GCD)를 출력한다
  52. print "GCD(#{val1}, #{val2}) = #{gcd}\n"




실행:

Command> ruby testWhile.rb
Using: ruby testWhile.rb [integer1] [integer2]
This finds the greatest common divisor of the given two integers.

Command> ruby testWhile.rb 23 25
GCD(23, 25) = 1

Command> ruby testWhile.rb 230 25
GCD(230, 25) = 5

Command> ruby testWhile.rb 230 125
GCD(230, 125) = 5

Command> ruby testWhile.rb -200 300
GCD(-200, 300) = 100




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

소스 파일명: testIf.rb

  1. =begin
  2.    Filename: testIf.rb
  3.    Purpose:  Example using the conditional control structure syntax
  4.                  if .... else ...
  5.    Execute: ruby testIf.rb [number]
  6. =end
  7. # 사용법을 보여주는 함수
  8. def printUsing()
  9.    print("Using: ruby testIf.rb [number]\n")
  10.    print("This determines whether the number is positive or not.\n")
  11. end
  12. # 명령행 인자의 개수가 1이 아니면 사용법을 보여준다.
  13. if (ARGV.length != 1)
  14.     printUsing()
  15.     exit(1)
  16. end
  17. # 명령행 인자의 스트링을 가져와서
  18. # 배정밀도 부동소수점수로 변환하여
  19. # 변수 val에 저장한다.
  20. val = ARGV[0].to_f
  21. # 변수 val에 저장된 값이 양수인지 음수인지 0인지를
  22. # 판단하는 if...else... 조건문
  23. if (val > 0.0)
  24.     print("#{val} is a positive number.\n")
  25. elsif (val < 0.0)
  26.     print("#{val} is a negative number.\n")
  27. else
  28.     print("#{val} is zero.\n")
  29. end


실행> ruby testIf.rb
Using: ruby testIf.rb [number]
This determines whether the number is positive or not.

실행> ruby testIf.rb 1.2
1.2 is a positive number.

실행> ruby testIf.rb -1.2
-1.2 is a negative number.

실행> ruby testIf.rb 0
0.0 is zero.



※ 위의 소스 코드는 JRuby로도 (수정 없이) 그대로 실행된다.

실행> jruby testIf.rb
Using: ruby testIf.rb [number]
This determines whether the number is positive or not.

실행> jruby testIf.rb 3.14
3.14 is a positive number.

실행> jruby testIf.rb -3.14
-3.14 is a negative number.

실행> jruby testIf.rb 0
0.0 is zero.




Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,
Ruby 언어에서는 명령행 인자를 처리하는 변수로 ARGV를 미리 지정하여 놓았다.


소스 파일명: testArguments.rb
  1. # 명령행 인자(command-line argument) 개수 출력
  2. print("Count of arguments: #{ARGV.length}\n")
  3. sum = 0.0
  4. for i in 0...ARGV.length
  5.     # 스트링을 부동소수점수로 변환하여 누적
  6.     sum += ARGV[i].to_f
  7. end
  8. # 누적된 값을 출력
  9. print("The sum of arguments is %g\n" % sum)


실행> ruby testArguments.rb 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> ruby testArguments.rb 1 2 3 4.2
Count of arguments: 4
The sum of arguments is 10.2


※ 위의 소스 코드는 JRuby로도 수정 없이 그대로 실행된다.

실행> jruby testArguments.rb 1 2 3 4
Count of arguments: 4
The sum of arguments is 10


실행> jruby testArguments.rb 1 2 3 4.2
Count of arguments: 4
The sum of arguments is 10.2





Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,

Ruby 언어의 함수 정의 구문 양식은

       def functionName(parameters)
             block
       end

이다.

또 Ruby 언어의 전형적인 for 반복문 양식은

       for varName in Range
             block
       end

이다.



소스 파일명: forTest.rb
------------------------------[소스 시작]
def printDan(dan)
  for i in 1..9
    print "#{dan} x #{i} = #{dan*i}\n"
  end
end

printDan(2)
------------------------------[소스 끝]

실행> ruby forTest.rb      (또는 jruby forTest.rb)
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18





Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.

Posted by Scripter
,
컨솔에 문자 출력하는 ruby 구문은

       print "문자열(스트링)\n"

이다. 여기서 "\n"은 개행을 위해 추가된 개행(newline) 문자이다.
(JRuby의 문자 출력 구문도 위와 같다.)
 
소스 파일명: hello.rb
------------------------------[소스 시작]
print "Hello, world!\n"
------------------------------[소스 끝]

실행> ruby hello.rb
Hello, world!
Posted by Scripter
,