Notice
Recent Posts
Recent Comments
«   2025/04   »
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 30
Tags
more
Archives
Today
Total
관리 메뉴

INSPECT

[Ruby]2 본문

Ruby on Rails - 멋쟁이 사자처럼

[Ruby]2

INSPECT 2017. 1. 26. 14:35
Next!

The next keyword can be used to skip over certain steps in the loop. For instance, if we don't want to print out the even numbers, we can write:

for i in 1..5
  next if i % 2 == 0
  print i
end
  1. In the above example, we loop through the range of 1 through 5, assigning each number to i in turn.
  2. If the remainder of i / 2is zero, we go to the next iteration of the loop.
  3. Then we print the value of i. This line only prints out 13, and 5 because of the previous line.
ex)



* next에 지정된 것은 제외하므로 20부터 1씩 줄어드는 숫자의 목록 중 2로 나누었을 때 나머지가 1인 것을 제외한다. 즉 홀수를 제외하기 때문에 짝수만 나오게 된다.


The .times Iterator

The .times method is like a super compact for loop: it can perform a task on each item in an object a specified number of times.

For example, if we wanted to print out "Chunky bacon!" ten times, we might type

10.times { print "Chunky bacon!" }

We don't know why we would type that, but we could


참고 : codecademy.com

'Ruby on Rails - 멋쟁이 사자처럼' 카테고리의 다른 글

[Ruby] 4. Symbol  (0) 2017.01.28
[Ruby]3. Hash  (0) 2017.01.26
[Ruby] 1  (0) 2017.01.26
[CSS] 웹 폰트 사용하기  (0) 2017.01.20
[C9] css: command not found 오류  (0) 2017.01.20
Comments