ARRAY < OBJECT

array.each {|item| block } → array

Calls block once for each element in self, passing that element as a parameter. 

a = [ "a", "b", "c" ]
a.each {|x| print x, " -- " }

produces:

a -- b -- c --
모든원소들이 블럭을 한번씩 실행한다.







[출처 : http://www.ruby-doc.org/core/]

by 강이 | 2008/03/05 14:14 | ruby Document | 트랙백 | 덧글(0)

MODEULE ENUMABLE

enum.collect {| obj | block } => array
enum.map {| obj | block } => array


Returns a new array with the results of running block once for every element in enum.

(1..4).collect {|i| i*i } #=> [1, 4, 9, 16]
(1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"]
모든 요소가 블럭을 실행후 새로운 배열을 return한다.


enum.detect(ifnone = nil) {| obj | block } => obj or nil
enum.find(ifnone = nil) {| obj | block } => obj or nil

Passes each entry in enum to block. Returns the first for which block is not false. 
If no object matches, calls ifnone and returns its result when it is specified, or returns nil

(1..10).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> nil
(1..100).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> 35

find는 detect의 별칭이다. 각각의 요소를 블럭에 적용하고 결과가 true인 첫번째 값을 반환한다.


enum.inject(initial) {| memo, obj | block } => obj
enum.inject {| memo, obj | block } => obj

Combines the elements of enum by applying the block to an accumulator value (memo) and each element in turn.
At each step, memo is set to the value returned by the block. The first form lets you supply an initial value
for memo. The second form uses the first element of the collection as a the initial value
(and skips that element while iterating).

# Sum some numbers
(5..10).inject {|sum, n| sum + n } #=> 45
# Multiply some numbers
(5..10).inject(1) {|product, n| product * n } #=> 151200

# find the longest word
longest = %w{ cat sheep bear }.inject do |memo,word|
memo.length > word.length ? memo : word
end
longest #=> "sheep"

# find the length of the longest word
longest = %w{ cat sheep bear }.inject(0) do |memo,word|
memo >= word.length ? memo : word.length
end
longest #=> 5

요소들 각각에 값을 차례로 블럭에 적용후 memo에 적용하며, memo에 초기값을 설정할 수 있다.


[출처 : http://www.ruby-doc.org/core/]

by 강이 | 2008/03/04 14:40 | ruby Document | 트랙백 | 덧글(0)

STRING < OBJECT

str.squeeze([other_str]*) => new_str

Builds a set of characters from the other_str parameter(s) using the procedure described for String#count. 
Returns a new string where runs of the same character that occur in this set are replaced by a single character.
If no arguments are given, all runs of identical characters are replaced by a single character.

"yellow moon".squeeze #=> "yelow mon"
" now is the".squeeze(" ") #=> " now is the"
"putters shoot balls".squeeze("m-z") #=> "puters shot balls"

매개변수가 없으면 공백을, 있으면 해당 문자열의 중복된부분을 하나만 남기고 제거해준 후, 새로운 string을 return한다.


str.chomp(separator=$/) => new_str
Returns a new String with the given record separator removed from the end of str (if present). 
If $/ has not been changed from the default Ruby record separator,
then chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n).

"hello".chomp #=> "hello"
"hello\n".chomp #=> "hello"
"hello\r\n".chomp #=> "hello"
"hello\n\r".chomp #=> "hello\n"
"hello\r".chomp #=> "hello"
"hello \n there".chomp #=> "hello \n there"
"hello".chomp("llo") #=> "he"
해당문자열의 구분자,개행문자를 제거한 후 새로운 string을 생성한다.



[출처 : http://www.ruby-doc.org/core/]

by 강이 | 2008/03/04 13:26 | ruby Document | 트랙백 | 덧글(0)

◀ 이전 페이지 다음 페이지 ▶