Skip to content

Commit 8e2313c

Browse files
marocchinoriseshia
andauthored
Translate faq 5 (ko) (#3028)
* Translate faq 5 (ko) * Fix broken link (ko) * Apply suggestions from code review Co-authored-by: Shia <rise.shia@gmail.com> * Apply suggestions from code review Co-authored-by: Shia <rise.shia@gmail.com> --------- Co-authored-by: Shia <rise.shia@gmail.com>
1 parent 672b076 commit 8e2313c

File tree

7 files changed

+240
-8
lines changed

7 files changed

+240
-8
lines changed

ko/documentation/faq/1/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ header: |
1515
<span class="separator"> | </span>
1616
<a href="../4/" title="파트 4">4</a>
1717
<span class="separator"> | </span>
18-
<a href="/en/documentation/faq/5/" title="파트 5">5</a>
18+
<a href="../5/" title="파트 5">5</a>
1919
<span class="separator"> | </span>
2020
<a href="/en/documentation/faq/6/" title="파트 6">6</a>
2121
<span class="separator"> | </span>

ko/documentation/faq/2/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ header: |
1515
<span class="separator"> | </span>
1616
<a href="../4/" title="파트 4">4</a>
1717
<span class="separator"> | </span>
18-
<a href="/en/documentation/faq/5/" title="파트 5">5</a>
18+
<a href="../5/" title="파트 5">5</a>
1919
<span class="separator"> | </span>
2020
<a href="/en/documentation/faq/6/" title="파트 6">6</a>
2121
<span class="separator"> | </span>

ko/documentation/faq/3/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ header: |
1515
<span class="separator"> | </span>
1616
<a href="../4/" title="파트 4">4</a>
1717
<span class="separator"> | </span>
18-
<a href="/en/documentation/faq/5/" title="파트 5">5</a>
18+
<a href="../5/" title="파트 5">5</a>
1919
<span class="separator"> | </span>
2020
<a href="/en/documentation/faq/6/" title="파트 6">6</a>
2121
<span class="separator"> | </span>

ko/documentation/faq/4/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ header: |
1515
<span class="separator"> | </span>
1616
<strong>4</strong>
1717
<span class="separator"> | </span>
18-
<a href="/en/documentation/faq/5/" title="파트 5">5</a>
18+
<a href="../5/" title="파트 5">5</a>
1919
<span class="separator"> | </span>
2020
<a href="/en/documentation/faq/6/" title="파트 6">6</a>
2121
<span class="separator"> | </span>

ko/documentation/faq/5/index.md

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
---
2+
layout: page
3+
title: "공식 Ruby FAQ"
4+
lang: ko
5+
6+
header: |
7+
<div class="multi-page">
8+
<a href="../" title="Content">콘텐츠</a>
9+
<span class="separator"> | </span>
10+
<a href="../1/" title="파트 1">1</a>
11+
<span class="separator"> | </span>
12+
<a href="../2/" title="파트 2">2</a>
13+
<span class="separator"> | </span>
14+
<a href="../3/" title="파트 3">3</a>
15+
<span class="separator"> | </span>
16+
<a href="../4/" title="파트 4">4</a>
17+
<span class="separator"> | </span>
18+
<strong>5</strong>
19+
<span class="separator"> | </span>
20+
<a href="/en/documentation/faq/6/" title="파트 6">6</a>
21+
<span class="separator"> | </span>
22+
<a href="/en/documentation/faq/7/" title="파트 7">7</a>
23+
<span class="separator"> | </span>
24+
<a href="/en/documentation/faq/8/" title="파트 8">8</a>
25+
<span class="separator"> | </span>
26+
<a href="/en/documentation/faq/9/" title="파트 9">9</a>
27+
<span class="separator"> | </span>
28+
<a href="/en/documentation/faq/10/" title="파트 10">10</a>
29+
<span class="separator"> | </span>
30+
<a href="/en/documentation/faq/11/" title="파트 11">11</a>
31+
</div>
32+
<h1>공식 Ruby FAQ</h1>
33+
34+
---
35+
36+
{% include faq-notice.md %}
37+
38+
## 이터레이터
39+
40+
### 이터레이터란 무엇인가요?
41+
42+
이터레이터는 블록 또는 `Proc` 객체를 받는 메서드입니다. 소스 파일에서
43+
블록은 메서드 호출 직후에 위치합니다. 이터레이터는 사용자 정의 제어
44+
구조, 특히 루프를 생성하는 데 사용됩니다.
45+
46+
이터레이터가 어떻게 작동하는지 예시를 통해 살펴봅시다. 이터레이터는 다음과 같이
47+
컬렉션의 각 요소에 대해 동일한 작업을 반복하는 데 자주 사용됩니다.
48+
49+
~~~
50+
data = [1, 2, 3]
51+
data.each do |i|
52+
puts i
53+
end
54+
~~~
55+
56+
결과:
57+
58+
~~~
59+
1
60+
2
61+
3
62+
~~~
63+
64+
배열 `data`의 each 메서드에는 `do ... end` 블록이 전달되어 반복적으로
65+
실행됩니다. 호출할 때마다 블록은 배열의 연속적인 요소를 전달받습니다.
66+
67+
블록을 정의할 때 `do ... end` 대신 `{ ... }`를 쓸 수 있습니다.
68+
69+
~~~
70+
data = [1, 2, 3]
71+
data.each { |i|
72+
puts i
73+
}
74+
~~~
75+
76+
결과:
77+
78+
~~~
79+
1
80+
2
81+
3
82+
~~~
83+
84+
이 코드는 전 예제와 동일한 의미를 갖습니다. 그러나 경우에 따라 우선순위
85+
문제로 인해 `do ... end``{ ... }`가 다르게 동작할 수 있습니다.
86+
87+
~~~
88+
foobar a, b do ... end # foobar가 이터레이터.
89+
foobar a, b { ... } # b가 이터레이터.
90+
~~~
91+
92+
이는 `{ ... }``do ... end` 블록보다 앞의 표현식에 더 강하게 바인딩되기
93+
때문입니다. 첫 번째 예는 `foobar(a, b) do ... end`와 같고, 두 번째 예는
94+
`foobar(a, b { ... })`와 같습니다.
95+
96+
### 블록을 이터레이터에 전달하려면 어떻게 해야 하나요?
97+
98+
반복자 호출 뒤에 블록을 배치하기만 하면 됩니다. `Proc`을 참조하는 변수나 상수
99+
이름 앞에 `&`를 붙여 `Proc` 객체를 전달할 수도 있습니다.
100+
101+
### 블록은 이터레이터에서 어떻게 사용되나요?
102+
103+
{% include faq-out-of-date.html %}
104+
105+
이터레이터 메서드에서 블록을 실행하는 방법에는 세 가지가 있습니다:
106+
(1) `yield` 제어 구조, (2) `call`을 사용하여 (블록으로 만든) `Proc` 인자를
107+
호출하는 것, (3) `Proc.new`를 사용한 후 호출하는 것 등이 있습니다.
108+
109+
`yield` 문은 블록을 호출하며, 하나 이상의 인수를 전달할 수 있습니다.
110+
111+
~~~
112+
def my_iterator
113+
yield 1, 2
114+
end
115+
116+
my_iterator {|a, b| puts a, b }
117+
~~~
118+
119+
결과:
120+
121+
~~~
122+
1
123+
2
124+
~~~
125+
126+
메서드 정의에 블록 인수가 있는 경우(마지막 형식 매개 변수 앞에 앰퍼샌드(`&`)가
127+
붙은 경우) `Proc` 객체로 변환된 첨부된 블록을 받습니다. 이것은
128+
`proc.call(args)`을 사용하여 호출할 수 있습니다.
129+
130+
~~~
131+
def my_iterator(&b)
132+
b.call(1, 2)
133+
end
134+
135+
my_iterator {|a, b| puts a, b }
136+
~~~
137+
138+
결과:
139+
140+
~~~
141+
1
142+
2
143+
~~~
144+
145+
이터레이터 정의에서 `Proc.new`(또는 동치인 `proc`, `lambda` 호출)를 사용하면
146+
메서드에 주어진 블록을 인수로 받아 프로시저 객체를 생성합니다. (`proc`
147+
`lambda`는 사실상 동의어입니다.)
148+
149+
_[업데이트 필요: `lambda`는 약간 다른 방식으로 작동하며 블록 없이 Proc 객체를
150+
생성하려고 시도했습니다(`tried to create Proc object without a block`)라는
151+
경고가 표시됩니다.]_
152+
153+
~~~
154+
def my_iterator
155+
Proc.new.call(3, 4)
156+
proc.call(5, 6)
157+
lambda.call(7, 8)
158+
end
159+
160+
my_iterator {|a, b| puts a, b }
161+
~~~
162+
163+
결과:
164+
165+
~~~
166+
3
167+
4
168+
5
169+
6
170+
7
171+
8
172+
~~~
173+
174+
놀랍게도 `Proc.new`와 친구들은 메서드에 연결된 블록을 전혀 소비하지 않으며,
175+
`Proc.new`를 호출할 때마다 동일한 블록에서 새 프로시저 객체를 생성합니다.
176+
177+
블록이 메서드와 연관되어 있는지 여부는 `block_given?`을 호출하여 확인할 수
178+
있습니다.
179+
180+
### 블록이 없는 `Proc.new`는 어떤 기능을 하나요?
181+
182+
블록이 없는 `Proc.new`는 프로시저 객체를 생성할 수 없으며 오류가 발생합니다.
183+
그러나 메서드 정의에서 블록이 없는 `Proc.new`는 메서드가 호출될 때 블록이
184+
존재한다는 것을 의미하므로 오류가 발생하지 않습니다.
185+
186+
187+
### 이터레이터를 병렬로 실행하려면 어떻게 해야 하나요?
188+
189+
여기 [\[ruby-talk:5252\]][ruby-talk:5252]에서 Matz가 스레드를 사용하는 해결책을
190+
채택했습니다.
191+
192+
~~~
193+
require "thread"
194+
195+
def combine(*iterators)
196+
queues = []
197+
threads = []
198+
199+
iterators.each do |it|
200+
queue = SizedQueue.new(1)
201+
th = Thread.new(it, queue) do |i, q|
202+
send(i) {|x| q << x }
203+
end
204+
queues << queue
205+
threads << th
206+
end
207+
208+
loop do
209+
ary = []
210+
queues.each {|q| ary << q.pop }
211+
yield ary
212+
213+
iterators.size.times do |i|
214+
return if !threads[i].status && queues[i].empty?
215+
end
216+
end
217+
end
218+
219+
def it1
220+
yield 1; yield 2; yield 3
221+
end
222+
223+
def it2
224+
yield 4; yield 5; yield 6
225+
end
226+
227+
combine(:it1, :it2) do |x|
228+
# x is [1, 4], then [2, 5], then [3, 6]
229+
end
230+
~~~
231+
232+
[ruby-talk:5252]: https://blade.ruby-lang.org/ruby-talk/5252

ko/documentation/faq/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ header: |
1515
<span class="separator"> | </span>
1616
<a href="4/" title="파트 4">4</a>
1717
<span class="separator"> | </span>
18-
<a href="/en/documentation/faq/5/" title="파트 5">5</a>
18+
<a href="5/" title="파트 5">5</a>
1919
<span class="separator"> | </span>
2020
<a href="/en/documentation/faq/6/" title="파트 6">6</a>
2121
<span class="separator"> | </span>
@@ -53,7 +53,7 @@ Stollsteimer에게 감사드립니다.
5353
* [Ruby의 경쟁 상대는...?](2/)
5454
* [Ruby 설치하기](3/)
5555
* [변수, 상수, 인수](4/)
56-
* [이터레이터](/en/documentation/faq/5/)
56+
* [이터레이터](5/)
5757
* [구문](/en/documentation/faq/6/)
5858
* [메서드](/en/documentation/faq/7/)
5959
* [클래스 및 모듈](/en/documentation/faq/8/)

ko/documentation/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Ruby를 배울 수 있는 매뉴얼과 튜토리얼, 코딩할 때 도움이 되
1818

1919
### 초보자용 문서
2020

21-
[공식 FAQ](/en/documentation/faq/) (영문)
21+
[공식 FAQ](faq/)
2222
: 자주 묻는 질문들을 모았습니다.
2323

2424
[Ruby Koans][2] (영문)
@@ -173,7 +173,7 @@ Ruby를 코딩할 때 운영체제의 기본 편집기를 사용할 수 있습
173173
[kdevelop]: https://www.kdevelop.org/
174174
[kate]: https://kate-editor.org/
175175

176-
[ko-1]: http://docs.google.com/View?docid=ajb44wcvmjj8_2fg33f2
176+
[ko-1]: https://docs.google.com/document/d/15yEpi2ZMB2Lld5lA1TANt13SJ_cKygP314cqyKhELwQ/preview
177177
[ko-2]: https://web.archive.org/web/20190521035426/http://beyond.daesan.com/pages/ruby-faq
178178
[ko-3]: https://groups.google.com/forum/#!forum/rubykr
179179
[ko-4]: https://www.facebook.com/groups/rubykr/

0 commit comments

Comments
 (0)