2007년 5월 10일 목요일

Ruby 프로그래밍 시작(기초)

from http://www.ibm.com/developerworks/kr/library/l-ruby1.html
------------------------------------------------------------------------------------------------------


루비(Ruby) 프로그래밍

루비 변수, 쿼트, 어레이, 객체, 메소드

developerWorks
문서 옵션
이 페이지를 이메일로 보내기

이 페이지를 이메일로 보내기

JavaScript가 필요한 문서 옵션은 디스플레이되지 않습니다.


난이도 : 초급

Joshua D. Drake, 프로젝트 매니저, Command Prompt, Inc

2001 년 8 월 01 일

Linux Networking HOWTO, Linux PPP HOWTO, Linux Consultants HOWTO의 저자이자 Command Prompt, Inc 의 공동 창업자인 Joshua Drake가 Ruby 프로그래밍을 설명한다. 다른 언어 사용자라도 이 글을 통해 유용한 정보를 얻을 수 있을 것이다.

루비(Ruby)는 일본의 Yukihiro Matsumoto가 개발한 순수한 객체 지향 스크립팅 언어이다. 텍스트 프로세싱과 시스템 관리 태스크를 핸들하도록 디자인되었다.

아래 예제에서 보듯이, 펄 또는 PHP 같은 스크립팅 언어를 작성해 본 경험이 있는 사람이라면 루비 신택스도 익숙할 것이다. 그러나, 펄 또는 PHP와는 다르게, 라인 종결자로 세미 콜론(semi-colon)을 사용해야 한다. 어떤 개발자들은 이런 사실 때문에 혼란스러워 할 수도 있지만 실제로 이것은 개발 속도를 높일 수 있다.


Listing 1. Hello world

#!/usr/bin/ruby
#
# My first ruby program
#
print "Hello World\n"


Listing 2. 아웃풋 변경하기

#!/usr/bin/ruby
#
# My first ruby program
#

# Declare our salutation

$salut = "Good Bye World\n"

# Prepare statements

 print "Hello World\n"
 print $salut

Using the $salut 변수를 사용하여 언제라도 두 번째 print 문장을 쉽게 변경할 수 있다. 프로그램의 어떤 지점에서도 변수를 선언할 수 있다.


Listing 3. 변수 선언하기

#!/usr/bin/ruby
#
# My first ruby program
#

$salut = "Good Bye World\n"

 print "Hello World\n"
 print $salut

#
# Declare a new value for $salut.
#

$salut = "Oh, I am sorry. I didn't mean to warn
you...\n"

 print "What do you mean Good Bye World?\n"
 print $salut

위 예제를 실행하면 아웃풋은 다음과 같다:


Hello World
Good Bye World
What do you mean Good Bye World?
Oh, I am sorry. I didn't mean to warn you...

변수 재지정(re-assignment)이 작동한다.




위로


싱글 쿼트(single quote) & 더블 쿼트(double quote)

대부분의 언어가 그렇듯, 루비도 싱글 쿼트와 더블 쿼트가 구별된다. 루비에서 더블 쿼트는 "루비는 쿼트 안에 있는 모든 값을 인터프리팅한다"는 것을 의미한다:


print "Hello World\n"

루비 인터프리터는 \n 을 이스케이핑 하지 않고 대신 새로운 라인을 STDOUT (Standard Out)에 프린트 할 것이다. 하지만 싱글 쿼트로 같은 문장을 실행시키면 다음과 같다:


print 'Hello World\n'

루비는 다음과 같이 아웃풋을 만들것이다:


Hello World\n

새로운 라인이 프린트되지 않았다는 것을 주목하라. 대신 루비는 전체 문장이 리터럴(literal)이라고 생각한다. 이것은 펄이 사용하는 것과 같은 기능이다.




위로


단어 수학

루비에서 모든 스트링은 연산자를 사용할 수 있다. 다시 말해서 단어에 대해 수학을 사용할 수 있다는 의미이다. 예를 들어:


$salut = $salut * 3

결과는 다음과 같다:


Oh, I am sorry. I didn't mean to warn you...
Oh, I am sorry. I didn't mean to warn you...
Oh, I am sorry. I didn't mean to warn you...

그리고


$salut = $salut + " 10, 9, 8..."

 print $salut

다음과 같이 결과가 나온다:


Oh, I am sorry. I didn't mean to warn you...
Oh, I am sorry. I didn't mean to warn you...
Oh, I am sorry. I didn't mean to warn you...
 10, 9, 8...

변수가 세번 프린트되었지만, "10, 9, 8..."은 프로그램의 끝에 한 번만 프린트되었다. 이유는 무엇일까?

이유는 "10, 9, 8..." 을 변수의 끝에 추가했기 때문이다. 변수에게 "10, 9, 8..."을 각 라인에 추가하라고 명령하지 않았다.

"What do you mean Good Bye World? \n" 에서 "Hello World\n" 로 바꾸었을 때 설명했지만, 일단 변수가 지정되면 그 변수가 재 지정(reassigned)되지 않는 한 정적(static)이다. 하지만 이것은 멀티플라이어(multiplier) 또는 연결(concatenation)이 변수와 함께 사용되는 경우를 의미하지는 않는다. 변수와 함께 연결(+ 표시)을 사용하면 이전에 지정된 변수와 지정된 변수에 추가된 스트링을 변수가 상속받을 수 있다. 우리의 경우, " 10, 9, 8..." 이다.




위로


어레이

$salut 변수를 여러번 재지정해야 한다면? 대부분 그런일은 발생하기 마련이다. 따라서 변수를 재지정하는 대신, 모든 변수 값을 어레이에 놓을 수 있다.

어레이는 각 변수의 값이 개별적으로 어드레스(address) 할 수 있도록 한다. 다음 예제를 보자:


$salut = ['Hello World','Good Bye World','What do you
mean Good Bye World?']

 print $salut

위에 대한 결과 아웃풋은 다음과 같다:


Hello WorldGood Bye WorldWhat do you mean Good Bye
World?

원하던 아웃풋이 아니다. 공간이나 새로운 라인이 없다. 따라서 우리는 어떤 부분의 어레이를 디스플레이 할지 정할 수 있고 아웃풋을 읽을 수 있도록 하기 위해서 연결(concatenation)을 사용할 수 있다.


$salut = ['Hello World','Good Bye World','What do you
mean Good Bye World?']

 print $salut[0] + "\n"
 print $salut[1] + "\n"
 print $salut[2] + "\n"

아웃풋은 다음과 같다:


Hello World
Good Bye World
What do you mean Good Bye World?

코드를 분석해보자:


$salut = ['Hello World','Good Bye World','What do you
mean Good Bye World?']

루비에서는 다음의 값을 갖고 있는 salut 변수를 지정할 수 있다:

$salut = 0 1 2
Hello World Good Bye World What do you mean Good Bye World?

0

으로 시작하고 이것에서 부터 늘어난다. 어레이의 두 번째 값을 프린트하기 위해 다음과 같이 타이핑한다:


 print $salut[1]




위로


IF, ELSE 조건

루비에서 데이터를 나타내는 기초를 살펴보았다. 이제 루비 프로그램의 로직에 대해 살펴보자. 말하자면 프로그래머에게 받은 결과에 근거한 기본 함수작동을 수행하도록 지시하는 것이다. 또한 루비 프로그램이 사용자에게 받은 결과에 대한 조건을 수행하는 방법도 살펴 볼 것이다. 새로운 코드가 이 예제에 사용될 것이다.


Listing 4. 결과에 근거한 기본 함수 수행하기

$salut = ['Hello World','Good Bye World','What do you
mean Good Bye World?']

 print "When you enter the world, what do you say? "
 while enterWorld = STDIN.gets
   enterWorld.chop!
   if enterWorld == $salut[0]
     print "\n" + "Yes. Hello World would be
polite.\n"
     break
   else
     print "You say '", enterWorld, "'?!\n" + "You
humans are so rude!\n"
   end
  end

루비 개발의 새로운 면이 위 코드에 많이 소개되었다.


예제

#!/usr/bin/ruby
#
# My first interactive ruby script
#

# Define our main variable with an array

$salut = ['Hello World','Good Bye World','What do you
mean Good Bye World?']

# Print my first question

 print "When you enter the world, what do you say? "

# Create a while loop with the object enterWorld and
await data from
# standard input. The while loop will make sure that
ruby continues
# to process the application until the program tells
it to stop.

 while enterWorld = STDIN.gets

# Make sure we use the chop method on the enterWorld
object. The use
# of the chop method will insure that we strip new
lines and carriage
# returns from our input.

# You will notice that using the chop method has an
extra
# character. The ! allows the existing object to be
modified
# by the method. If you did not use the !, you would
have to redeclare
# the enterWorld object for the if condition to
correctly occur.

   enterWorld.chop!

# Begin the condition sequence. Basically, if
enterworld equals
# Hello World, which is 0, within the array, print
# a new line. Then print, "Yes, Hello World would be
polite." to the screen.

   if enterWorld == $salut[0]
     print "\n" + "Yes. Hello World would be
polite.\n"

# The break statement tells ruby to stop executing if
the previous
# condition is met. If we did not include this in our
while loop,
# the program would run continuously.

    break

# The else statement is used as the secondary
condition. In other
# words, if the first condition is not met, please do
the following.

   else
     print "You say '", enterWorld, "'?!\n" + "You
humans are so rude!\n"
   break

# The end statement is used to close a condition or
loop. In our case,
# it is being used to close both. We are first closing
our if
# condition statements and then stopping our while
loop.

   end
  end




위로


객체와 메소드

이 코드에 사용된 몇 가지의 기술과 방법들은 여러분에게 생소할 수도 있다. 루비는 객체 지향 프로그래밍(OOP) 언어이다. OOP를 사용한다는 것은 일반적으로 객체와 매소드 같은 아이템을 호출하는 것이다. 객체(object)는 콘테이너와 같다. 이것은 변수와 함수를 포함하고 있다. 매소드(method) 는 객체를 어드레싱하는 함수와 같은 것이다. 이전 예제에서, 객체와 메소드가 모두 실행되는 것을 보여줄 수 있다.


1

while enterWorld = STDIN.gets
   enterWorld.chop!

두 개의 객체와 두 개의 메소드가 있다. 첫 번째 객체는 enterWorld 이고 두 번째 객체는 STDIN 이다. enterWorld 객체는 사용자 정의 객체이고 STDIN 객체는 루비에 구현되어 있다.

예제에는 두 개의 메소드도 있다. 첫 번째는 gets 이고 두 번째는 chop!이다. 앞서 언급했지만 메소드는 객체를 어드레싱 한다. 특히 메소드는 객체 안에서 액션을 수행한다. gets 메소드를 사용하여 루비에게 STDIN을 얻도록(get) 명령할 수 있다. 루비가 STDIN 과 관련된 gets를 보면, 키보드 인풋과 캐리지 리턴(carriage return)을 기다릴 것이다. STDIN.gets은 사용자가 무언가를 타이핑하고 엔터를 치기를 기다린다.

두 번째 메소드인 chop!은 사용자 정의 객체인 enterWorld를 어드레싱 한다. chop! 메소드는 enterWorld에게 enterWorld 객체와 관련된 데이터에서 새로운 라인과 캐리지리턴으로 자르도록 (chop) 명령한다. 이전의 코드에서 chop! (또는 chomp!)을 사용하지 않는다면 "true"가 결코 될 수 없다.


 if enterWorld == $salut[0]

chop!를 사용하지 않고 $salut[0]이 실제로 $salut[0]\n와 같기 때문에 "false"가 된다. 새로운 라인은 STDIN 객체가 gets 메소드에서 받은 인풋에 의해 생겨난다. 캐리지 리턴을 사용하면 새로운 라인 문자들을 값의 끝에 추가하게 된다.




위로


결론

루비는 매우 강력하지만 사용하기가 쉽지만은 않다. C++, Perl, Python 프로그래밍 경험이 있다면 그러한 언어들과 루비와의 상당한 유사성을 발견하게 될 것이다 (특히 Python).




위로


참고자료




위로


필자소개

Joshua Drake는 Command Prompt, Inc의 공동 창립자이다. PostgreSQL과 리눅스 개발을 하고 있다. Linux Networking HOWTO, Linux PPP HOWTO, Linux Consultants HOWTO의 저자이기도 하다.

댓글 없음:

댓글 쓰기