루비를 설치하자!
오늘은 루비 상에서 아주 간단한 코딩을 해보는 것을 목표로 한다. 그렇다면 우선 루비가 설치되어 있어야하겠다.
# ruby -bash: ruby: command not found
이런, 루비가 설치되어 있지 않다.
루비 설치 방법은 https://www.ruby-lang.org/ko/documentation/installation/ 에서 자세히 확인할 수 있다. 오늘은 많은 방법 중에 "패키지 관리 시스템"을 이용하여 설치하려고 한다. 레드햇 계열 리눅스는 yum이라는 명령어를 이용하여 설치할 수 있다.
# yum install ruby Loaded plugins: fastestmirror Determining fastest mirrors * base: data.nicehosting.co.kr * extras: data.nicehosting.co.kr * updates: data.nicehosting.co.kr base | 3.7 kB 00:00 extras | 3.4 kB 00:00 extras/primary_db | 37 kB 00:00 updates | 3.4 kB 00:00 updates/primary_db | 4.3 MB 00:34 Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package ruby.x86_64 0:1.8.7.374-4.el6_6 will be installed --> Processing Dependency: ruby-libs = 1.8.7.374-4.el6_6 for package: ruby-1.8.7.374-4.el6_6.x86_64 --> Processing Dependency: libruby.so.1.8()(64bit) for package: ruby-1.8.7.374-4.el6_6.x86_64 --> Running transaction check ---> Package ruby-libs.x86_64 0:1.8.7.374-4.el6_6 will be installed --> Processing Dependency: libreadline.so.5()(64bit) for package: ruby-libs-1.8.7.374-4.el6_6.x86_64 --> Running transaction check ---> Package compat-readline5.x86_64 0:5.2-17.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================== Package Arch Version Repository Size =================================================================================================== Installing: ruby x86_64 1.8.7.374-4.el6_6 base 538 k Installing for dependencies: compat-readline5 x86_64 5.2-17.1.el6 base 130 k ruby-libs x86_64 1.8.7.374-4.el6_6 base 1.7 M Transaction Summary =================================================================================================== Install 3 Package(s) Total download size: 2.3 M Installed size: 7.8 M Is this ok [y/N]: y Is this ok [y/N]: y Downloading Packages: (1/3): compat-readline5-5.2-17.1.el6.x86_64.rpm | 130 kB 00:01 (2/3): ruby-1.8.7.374-4.el6_6.x86_64.rpm | 538 kB 00:04 (3/3): ruby-libs-1.8.7.374-4.el6_6.x86_64.rpm | 1.7 MB 00:13 --------------------------------------------------------------------------------------------------- Total 124 kB/s | 2.3 MB 00:18 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : compat-readline5-5.2-17.1.el6.x86_64 1/3 Installing : ruby-libs-1.8.7.374-4.el6_6.x86_64 2/3 Installing : ruby-1.8.7.374-4.el6_6.x86_64 3/3 Verifying : compat-readline5-5.2-17.1.el6.x86_64 1/3 Verifying : ruby-libs-1.8.7.374-4.el6_6.x86_64 2/3 Verifying : ruby-1.8.7.374-4.el6_6.x86_64 3/3 Installed: ruby.x86_64 0:1.8.7.374-4.el6_6 Dependency Installed: compat-readline5.x86_64 0:5.2-17.1.el6 ruby-libs.x86_64 0:1.8.7.374-4.el6_6 Complete!
Complete!가 나오면 잘 설치되었(을 것이)다. ruby를 실행하여 확인해보자.
# ruby (...아무 반응이 없음...)
아무 반응이 없으면 잘 된 것이다. 일단 대충 빠져나오자.
이번에는 -v 옵션을 추가하여 루비를 실행한다. 이를 통해 버전 정보를 알 수 있다.
# ruby -v ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
Hello World 출력하기
모든 언어의 기본 Hello World를 시도해 보자. 자바에 System.out.println이 있다면 루비에는 puts가 있다. 외우자. 그리고 아래와 같은 helloworld.rb 파일을 만들어 보자.
# cat helloworld.rb puts 'Hello World! by intersection3 @ sarc.io'
실행 결과는 다음과 같다.
# ruby helloworld.rb Hello World! by intersection3 @ sarc.io
커맨드 라인 Argument를 입력받아 출력하기
이번에는 커맨드 라인으로 Argument를 입력받아 출력하는 작업을 해보자. helloworld.rb 파일을 다음과 같이 수정해보자.
# cat helloworld.rb id = ARGV[0] site = ARGV[1] puts 'Hello World!' puts id puts site
실행 결과는 다음과 같다.
# ruby helloworld.rb intersection3 sarc.io Hello World! intersection3 sarc.io
물론 아래와 같이 처리할 수 있다.
# cat helloworld.rb id = ARGV[0] site = ARGV[1] puts 'Hello World!, ' + id + ' @ ' + site
결과는 이렇다.
# ruby helloworld.rb intersection3 sarc.io Hello World!, intersection3 @ sarc.io
반복문
먼저 while을 이용하여 반복문을 구현하려고 한다.
id = ARGV[0] site = ARGV[1] i = 0 while i < 10 puts "Hello World!, intersection#{i}" + " @ " + site i += 1 end
달라진 것이 있다. 앞서는 ' ' (single quotation)을 사용했는데 이번에는 " " (double quotation)을 사용했다. 그 이유는 #{i}와 같이 변수를 처리하기 위함이다. 쉘 스크립트에서도 echo 내에 변수를 사용하려면 " "을 사용해야 한다.
결과이다.
# ruby helloworld.rb intersection3 sarc.io Hello World!, intersection0 @ sarc.io Hello World!, intersection1 @ sarc.io Hello World!, intersection2 @ sarc.io Hello World!, intersection3 @ sarc.io Hello World!, intersection4 @ sarc.io Hello World!, intersection5 @ sarc.io Hello World!, intersection6 @ sarc.io Hello World!, intersection7 @ sarc.io Hello World!, intersection8 @ sarc.io Hello World!, intersection9 @ sarc.io
다음은 for를 이용한 반복문이다.
id = ARGV[0] site = ARGV[1] for i in 0..9 puts "Hello World!, intersection#{i}" + " @ " + site end
결과이다.
# ruby helloworld.rb intersection3 sarc.io Hello World!, intersection0 @ sarc.io Hello World!, intersection1 @ sarc.io Hello World!, intersection2 @ sarc.io Hello World!, intersection3 @ sarc.io Hello World!, intersection4 @ sarc.io Hello World!, intersection5 @ sarc.io Hello World!, intersection6 @ sarc.io Hello World!, intersection7 @ sarc.io Hello World!, intersection8 @ sarc.io Hello World!, intersection9 @ sarc.io
오늘은 루비를 깔고 간단히 찍어보고 돌려보는 것까지 해봤다. 다음에는 좀 더 고급진 루비 코딩을 할 수 있게 되길 우리 모두 바라자.