2006/12/14 09:43 Developer

Ruby Basic Syntax

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

스크립트 언어의 특징중 간단함을 느낄수 있다. 쉘스크립트랑 거의 유사하다.

i = 0
  while i< 10
  i=i+1
  end
  puts i    # 10

loop문이다. 이것 역시 단순하다. 1을 10번 더하면 10이다.






Posted by ologist
 TAG

블로그 이미지
ologist

공지사항

Yesterday170
Today60
Total33,501

달력

 « |  » 2012.02
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

최근에 받은 트랙백

글 보관함