One of the advantages of learning a new language is being exposed to new idioms and new approaches to solving old problems. In this talk, we will introduce the Ruby language with particular focus on the idioms and concepts that are different from what is found in Java.
We will introduce concepts such as closures, continuations and meta programming. We will also examine powerful techniques that are practically impossible in Java due to its compile time binding of types.
No experience with Ruby is assumed although an understanding of Java would be helpful.
This talk was given at the Toronto Java Users Group in April 2008
Introduction to Ruby on Rails by Rails Core alumnus Thomas Fuchs.
Originally a 3-4 hour tutorial, 150+ slides about Rails, Ruby and the ecosystem around it.
Introduction to Ruby and Introduction to Ruby on Rails basic concepts for beginners. The google presentation is even better in full screen https://docs.google.com/presentation/d/1EE0VuB_PkD2-8j5JNs6CUQHb4J9ToIgC7-IxYTojiS0/pub?start=false&loop=false&delayms=3000#slide=id.p
4. 1. 변수 – (1) 참조
변수가 저장하는 것은 객체로의 참조이다.
객체 자체나, 값 자체를 저장하는 것이 아니다.
따라서 여러 변수가 동일한 객체를 참조 할 수 있다.
# 1 – p113
A = ‚power‛
B=A
A[1] = ?Q
Puts A => ‚pQwer‛
Puts B => ‚pQwer‛
5. 1. 변수 – (1) 참조
#2 – p115
def describe(name)
puts name
name[2] = ?p
puts name
end
A = ‚Yahoo‛
describe(A)
puts A => ‚Yapoo‛
6. 1. 변수 – (1) 참조
#3 – p115
#2 에서 변하는 것을 막기 위해서는 복사하여 사용해야 한다.
def describe(name)
name = name.dup
puts name
name[2] = ?p
puts name
end
7. 1. 변수 – (2) 변수의 종류
Ruby 에서는 ‘변수선언’ 이 없다.
때문에, 인터프리터가 변수명의 첪 문자를 보고 변수의 종류를 판단하게 된다.
_variable : 지역변수 : 참조하기 젂에 대입해야 함.
v(소)ariable : 지역변수 : 상동
@variable : 인스턴수 변수 : 초기값 nil
@@variable : 클래스 변수 : 참조하기 젂에 대입해야 함.
$variable : 젂역변수 : 초기값 nil
V(대)ariable : 상수 : 참조하기 젂에 대입해야함.
8. 1. 변수 – (3) 지역변수
def a(origin)
value = origin+1
puts value
end
def b(origin)
value = origin-1
puts value
end
a(2) => 3; a(2) => 3; b(2) => 3;
puts value => Error
## 따라서, def 문 안에서 쓰인 value 들은 모두 각각 별개이다.
9. 1. 변수 – (4) 인스턴스 변수
인스턴스 변수는 특정 객체에 속하는 변수 인데, 동일한 객체의 메소드끼리 서로
공유한다.
** 지역변수의 경우는 해당 메소드 안에서만 사용이 가능하지만, 인스턴스 변수의
경우에는 메소드들 끼리 상태를 공유하므로, A 메소드에서 값을 바꾸었을때 B 메소드
에서도 바뀐 값을 사용할 수 있게 된다.
자세한 내용은 8장에서.
10. 1. 변수 – (5) 클래스 변수
클래스 변수는 기본적으로 클래스에 속하는 변수를 말한다.
자세한 내용은 (4) 의 인스턴스 변수와 함께 8장에서.
11. 1. 변수 – (6) 젂역변수
$variable : 젂역변수.
언제 어디서든 참조가 가능한 변수이다. 어디서든 값을 변경시킬 수 있다.
남발하게 되면 모든 모듈이 그 변수를 통해 결합하게 되므로, 유연성이 떨어진다.
** 내장변수
인터프리터의 상태나 동작을 제어하기 위한 플래그를 저장하고 있다.
$stdout - 출력 메소드의 default 출력 위치를 나타낸다.
$: - 검색경로를 저장한다.
$1, $2 … - 정규표현 매치를 저장한다.
12. 1. 변수 – (7) 상수
변경되지 않는 변수 이다.
변경이 불가능하지는 않으나, 변경할 시 경고가 출력된다.
13. 1. 변수 – (8) 의사변수
nil :빆값
true : 참
false : 거짓
slef : 자기 자싞 (this 와 비슷하다.)
__FILE__ : 해당 위치의 소스파일명. 변경 불가능한 문자열을 참조한다.
__LINE__ : 해당 위치의 행번호를 나타낸다. Integer 객체를 참조한다.
__ENCODING__ : 해당위치의 소스파일 인코딩을 저장하고 있다.
** 소문자이므로 인터프리터에서 지역변수로 인식을 해야 하지만, 실제로는 사용자가
값을 대입 할 수 없다.
14. 1. 변수 – (9) 식별자와 변수명
Ruby 에서 변수명에는 첪 글자를 제외한 모든 부분에서 _(언더바) 와 출력 가능한
문자들 외 ASCII 문자들은 사용할 수 없다.
Big_endian, bigEndian, 빅엔디언 : 모두 가능함.
Little-power, quinbus@flestrin : ASCII 기호 -, @ 가 들어갔으므로 불가능함.
17. 2. 연산자 – (1) 재정의 가능한 연산자
1+2 라는 식은 1.+(2) 라는 메소드 호출이다.
다음 연산자는 실제로는 메소드 이므로 재정의 가능하다.
| ^ & <=> == === =~ > >= < <= << >>
+ - * / % ** ~ +@ -@ [] []=
** 자기대입 연산자는 재정의 될 수 없다.
+= -= *= /= %= **= <<= >>= |= &= ^= &&= ||= 등.
하지만 a += b 는 a = a+b 를 의미하므로, + 를 재정의 할 시 실질적인 의미가
변경 될 수 있다.
18. 2. 연산자 – (1) 재정의 가능한 연산자
** 부정연산자는 재정의가 가능하지만, 거의 쓸 일이 없다
!= 는 == 의 부정.
!~ 는 =~ 의 부정이다.
19. 2. 연산자 – (2) 재정의 불가능한 연산자
앞에서 말한 자기대입 연산자 및 다음의 연산자는 재정의 할 수 없다.
= ?: .. … ! not && and || or ::
** 대입
a = b = c = 1 의 경우 a = (b = (c = 1)) 로 해석된다.
a, b, c = 1, 2, 3 의 경우 a = 1, b = 2, c = 3 과 같다.
a , b = b, a 의 경우 값이 서로 변경된다.
** 양변의 항목 개수가 일치하지 않는 경우, 남는 값은 버려지고, 부족한 값은
nil 로 채워진다.
20. 2. 연산자 – (2) 재정의 불가능한 연산자
** 배열 젂개
a, *b = 1, 2, 3, 4, 5
puts b => [2, 3, 4, 5] – 배열
Array = [1, 2, 3]
a, b, c = *Array
puts a => 1
puts b => 2
puts c => 3
21. 2. 연산자 – (3) 논리 연산자
논리 연산자는 진위 판별을 다루기 위한 재정의 불가능한 연산자이다.
주로 if 식등의 조건 부분을 구성하기 위해 사용된다.
if a == 1 and b == 2
puts ‘ok’
end
22. 2. 연산자 – (3) 논리 연산자
a && b : a 가 참이면 무조건 b, a 가 거짓이면 무조건 a
a || b : a 가 참이면 무조건 a, a 가 거짓이면 무조건 b
** 초기화 관용구
a ||= default_value
일 때, a 가 값을 가진 상태면 아무 작업을 하지 않으나, a 가 빆 값일경우
default_value 가 대입된다.
23. 2. 연산자 – (4) 범위 연산자
.. 와 ... 의 두 종류 연산자가 있다.
a .. b 가 생성하는 범위에는 b가 포함된다. [a,b]
a ... b 가 생성하는 범위에는 b가 포함되지 않는다. [a,b)
24. 2. 연산자 – (5) 조건 연산자
a?b:c
는 a가 참이면 b로 결정되고, a가 거짓이면 c로 결정된다.
Var = con ? true : false
는 다음과 같다.
Var = if con then true else false end
26. 3. 제어식
일반적인 언어와 다르게, ‘제어문’ 이라고 하지 않는 이유는 제어식이 값을 반환하기
때문이다.
thought = if sample.color == ‚green‛ then
‚danger‛
else
‚undefined‛
end
의 경우 변수 thought 에 ‚danger‛ 또는 ‚undefined‛ 를 대입한다.
27. 3. 제어식 – (1) if 식
if condition then
do_something
end
-> Condition 이 true 일 경우 do_something 메소드를 실행한다.
if condition then
do_something
else
do_something_other
end
-> condition 이 true 일 경우 do_something 메소드를 실행하고,
그 이외의 경우 do_something_other 메소드를 실행한다.
28. 3. 제어식 – (1) if 식
if condition then
do_something1
elsif condition2 then
do_something2
elsif condition3 then
do_something3
else
do_something_other
end
-> 각조건에 맞게 실행한다. 예외는 do_something_other 메소드를 실행.
29. 3. 제어식 – (1) if 식
unless condition
do_something
end
if 와 반대되는 개념으로, condition 이 거짓일 때 수행된다.
역시 elsif , else 를 사용할 수 있다.
30. 3. 제어식 – (2) case 식(#1)
Array = [4, 5]
case value
when 1 then
do_something1 # value 가 1인경우
when 2, 3 then
do_something2 # value 가 2 또는 3인 경우
when *Array # then 은 생략가능
do_something3 # 4 또는 5 인경우
else
do_something_other
end
31. 3. 제어식 – (2) case 식(#1)
value = 3
case value
when 0 then ‘0’
when 1..9 then ‘1자리’
when 10..99 then ‘2자리’
end
value = ‚3‛
case value
when ‘0’ then ‘0’
when /AdZ/ then ‘1자리’
when /Ad{2}Z/ then ‘2자리’
end
32. 3. 제어식 – (3) case 식(#2)
if number.prime? then process_prime(number)
elsif number.format? then process_carmichel(number)
elsif number.odd? then process_odd_composite(number)
else process_even_composite(number)
end
는 아래와 같다
case
when number.prime? then process_prime(number)
when number.format? then process_carmichel(number)
when number.odd? then process_odd_composite(number)
else process_even_composite(number)
end
33. 3. 제어식 – (4) while 식
while condition do # do는 생략 가능
do_something
end
**후치 while 식
def more?
gets.chomp != ‚ok‛
end
begin
$stdout.print ‚만족하면 ‘ok’ 라고 입력하세요:‛
end while more?
34. 3. 제어식 – (4) while 식
** while 수식자
do_something while condition
조건이 참인동안에만 do_something 을 반복한다.
** until
if 에 unless 가 있듯 while 에는 until 이 있는데, 조건식이 성립 안할 때 반복
하며, 성립하면 종료된다.
until condition
do_something
end
35. 3. 제어식 – (5) for 식
** while 수식자
for i in [1, 2, 3] do # do 는 생략가능
puts i
end
위 코드는 다음과 같다
[1, 2, 3].each do |i|
puts I
end
For 식은 내부적으로 each iterator 를 호출하므로 each 만으로 충분 하다.
36. 3. 제어식 – (6) iterator
** loop
내장 함수 loop 는 무한루프를 제공하는 iterator 이다. 탈출식이 없는 한, 계속해서
돌아가므로 골치 아플 수 있다.
loop do
puts ‚kk‛
end
37. 3. 제어식 – (6) iterator
** times
Integer 클래스의 times 메소드는 integer 객체가 나타내는 횟수만큼 블록을
실행 시킨다.
3.times{ puts ‚Yahoo‛ }
이 메소드는 내부 카운터를 블록에 넘겨주므로 카운터를 이용할 수 도 있다.
3.times{ |i| puts i }
38. 3. 제어식 – (6) iterator
** upto
1.upto(3) do |i| puts i end
# 1~ 3 출력
** downto
5.downto(2) do |i| puts i end
# 5, 4, 3, 2 출력
39. 3. 제어식 – (7) escape (탈출)
** break
현재의 반복구조로부터 탈출한다.
여러 겹의 반복일 경우 자싞이 속한 Scope 의 반복을 빠져나갂다.
** next
아래의 메소드들을 실행하지 않고, 다음 반복으로 넘어갂다.
** redo
자싞이 속한 scope 의 반복을 한번 더 한다. 조건의 여부는 따지지 않는다.
41. 4. 예외처리 – (1) rescue 수식자
갂결한 예외처리 기법이다.
do_something rescue error_handling
좌변을 실행하는 중에 예외가 발생하면 우변을 실행한다.
예외 클래스를 지정하거나 예외 객체를 포착할 수 는 없다.
42. 4. 예외처리 – (2) raise
사용자가 명시적으로 예외를 발생시킬때 사용한다.
raise ArgumentError, ‘message’
이 경우에 에러메세지로 ‘message’를 갖는 ArgumentError 예외를 발생시킨다.
raise ArgumentError # 에러메세지 없이 ArgumentError 발생
raise ‘message’ # message라는 메시지로 RuntimeError 발생
raise # 에러메세지 없이 RuntimeError 발생
44. 5. 대역탈출
break 는 해당 scope 에서 (가장 안쪽에서) 반복을 탈출한다.
중첩된 반복의 경우에는 catch 와 throw 를 사용한다.
catch(:exit) {
loop do # 무한루프
loop do # 무한루프
throw :exit # 여기서 탈출
end
end
}
throw 의 파라메터로는 심볼 또는 문자열을 넘긴다.
만약 상응하는 catch를 찾지 못하게 되면 ArgumentError 가 발생한다.