프로그래밍/Ruby 26

80컬럼 컨솔에 19단표 출력하기 예제 for Ruby

다음은 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. d..

(최대공약수 구하기) while... 반복문 예제 for Ruby

소스 파일명: testWhile.rb # # Filename: testWhile.rb # # Purpose: Example using the while loop syntax # while .... # # Execute: ruby testWhile.rb -200 300 # # 사용법 표시 def printUsage() print "Using: ruby testWhile.rb [integer1] [integer2]\n" print "This finds the greatest common divisor of the given two integers.\n" end if (ARGV.length != 2) printUsage() exit(1) end # ----------------------------------..

if...else... 조건문 사용 예제 for Ruby

소스 파일명: testIf.rb =begin Filename: testIf.rb Purpose: Example using the conditional control structure syntax if .... else ... Execute: ruby testIf.rb [number] =end # 사용법을 보여주는 함수 def printUsing() print("Using: ruby testIf.rb [number]\n") print("This determines whether the number is positive or not.\n") end # 명령행 인자의 개수가 1이 아니면 사용법을 보여준다. if (ARGV.length != 1) printUsing() exit(1) end # 명령행 인자의 스..

명령행 인자 처리 예제 for Ruby and JRuby

Ruby 언어에서는 명령행 인자를 처리하는 변수로 ARGV를 미리 지정하여 놓았다. 소스 파일명: testArguments.rb # 명령행 인자(command-line argument) 개수 출력 print("Count of arguments: #{ARGV.length}\n") sum = 0.0 for i in 0...ARGV.length # 스트링을 부동소수점수로 변환하여 누적 sum += ARGV[i].to_f end # 누적된 값을 출력 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 testArgument..

구구단 풀력 예제 for Ruby and JRuby

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 =..