Ruby의 기본적인 syntax를 알아보자
기본적인 Rule
- Ruby는 문장 끝에 세미콜론(;)을 붙이지 않는다.
- 한줄 주석은 #
- 전역변수 앞에는 $
- 인스턴스 변수앞에는 @
- 클래스 변수앞에는 @@
Arrays and Hashes
a = [1, 2, 3]
puts a[1] # array
struct = Hash.new(1) # default value
puts struct['key']
struct['key'] = struct['key'] + 1
puts struct['key']
별설명이 필요가 없다. 예제만 보면 대략감으로 알수 있다.
특징적인 것은 Hash.new(defaultValue)라는 것이다.
Control Structures
가장 중요한 것중의 하나인 분기하는 것이다.
if( struct['key'] == 2)
puts "succees : #{struct['key']}"
else
puts "error"
end
puts "succees : #{struct['key']}"
else
puts "error"
end
스크립트 언어의 특징중 간단함을 느낄수 있다. 쉘스크립트랑 거의 유사하다.
i = 0
while i< 10
i=i+1
end
puts i # 10
while i< 10
i=i+1
end
puts i # 10
loop문이다. 이것 역시 단순하다. 1을 10번 더하면 10이다.
