Method

Method

Iterator

: Iterators are nothing but methods supported by collections. Objects that store a group of data members are called collections. In Ruby, arrays and hashes can be termed collections. Iterators return all the elements of a collection, one after the other. We will be discussing two iterators here, each and collect. Let's look at these in detail.

  • .each

    : which can apply an expression to each element of an object, one at a time.

    object.each { |item| # Do something }
    object.each do |item| # Do something end
    array = [1, 2, 3, 4, 5]
    #=> [1, 2, 3, 4, 5]
    array.each do |x|
      x += 10
      puts x
    end
    # 11
    # 12
    # 13
    # 14
    # 15
    # [1, 2, 3, 4, 5]

    ๋ฐฐ์—ด์˜ ๊ฐ๊ฐ์˜ ์›์†Œ๊ฐ€ 10์”ฉ ์ฆ๊ฐ€ ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

  • .times : ์•ž์˜ ์ˆ˜ ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ผ๋Š” ๋œป์ด๋‹ค.

    3.times { puts "hi" }
    # hi
    # hi
    # hi
    #=> 3
  • .split :it takes in a string and returns an array (๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•ด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.)

    a = "Hello, This is example"
    #=> "Hello, This is example"
    a.split(" ")
    #=> ["Hello,", "This", "is", "example"]
  • .sort_by : ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

  • .reverse : ์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ

  • .sort : ์ •๋ ฌ

  • .to_s : string์œผ๋กœ data type ๋ณ€๊ฒฝ

  • .to_sym or .intern : symbol๋กœ data type ๋ณ€๊ฒฝ

    :exam.to_s
    # ==> "exam"
    "exam".to_sym
    # ==> :exam
  • .push or << or + : ๋ฐฐ์—ด,๋ฌธ์ž์—ด์— ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉ

    ๋ฌธ์ž์—ด์— ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” string ์ด ์•„๋‹Œ ๊ฒƒ์€ .to_s๋ฅผ ์ด์šฉํ•ด์„œ string์œผ๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค. ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” #{}์„ ํ†ตํ•ด์„œ ํ•ด์•ผํ•œ๋‹ค.

  • .select : filter {}์•ˆ์— ์žˆ๋Š” ์กฐ๊ฑด๋Œ€๋กœ ๊ฑฐ๋ฅธ๋‹ค.

    grades = {
      alice: 100,
        bob: 92,
        chris: 95,
        dave: 97
    }
    grades.select {|name, grade| grade < 97}
    # ==> {:bob=>92, :chris=>95}
    grades.select { |k, v| k == :alice }
    # ==> {:alice=>100}
  • .each_key : key๋งŒ ๋ฝ‘๋Š” hash methods

  • .each_value : hash ์—์„œ value๋งŒ ๋ฝ‘์•„๋‚ด๋Š” hash methods

  • .delete : hash์—์„œ ์‚ญ์ œ๋จ.

  • .upto , .downto

    for i in 1..10
        print i, " "
    end
    # => 1 2 3 4 5 6 7 8 9 10
    # Compare this with upto, which does exactly the same thing:
    1.upto(10) { |i| print i, " " } 
    # => 1 2 3 4 5 6 7 8 9 10
  • .respond_to? : takes a symbol and returns true if an object can receive that method and false otherwise

    [1, 2, 3].respond_to?(:push)
    # => true (.push on an array object)
    [1, 2, 3].respond_to?(:to_sym)
    # => false (can't turn an array into a symbol.)
  • .collect or .map : takes a block and applies the expression in the block to every element.

  • .floor : rounds a float

  • .is_a? Object : object ์ธ์ง€ ํ™•์ธ

    :hello.is_a? Symbol
    # ==> true

Methods(ํ•จ์ˆ˜)

def method_name(argument, *argu)
    # Do something!
    return someting
end

method_name(argument)

์—ฌ๋Ÿฌ๊ฐœ์˜ argument๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ๋Š” *๋ฅผ ์ด์šฉํ•œ๋‹ค.(splat) ํ•จ์ˆ˜ํ˜ธ์ถœ์€ ํ•จ์ˆ˜์ด๋ฆ„์„ ์ ์–ด ํ˜ธ์ถœํ•œ๋‹ค. return์€ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์ด๋‹ค.

def hello(names)
    names.each do |n|
        puts "Hello, #{n.upcase}"
    end
end

rubies = %w[MRI jruby rubinius]

hello(rubies)

parameters and arguments

arguments๋Š” ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์„ ๋œปํ•˜๋ฉฐ, parameters๋Š” ๊ทธ ์ด๋ฆ„์„ ๋œปํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด

def add(a,b)
    puts a+b
end

add(2,4)

์—์„œ a,b๋Š” parameter์ด๊ณ , 2,4๋Š” arguments์ด๋‹ค.

combined comparison operator <=>

๋‘๊ฐœ๊ฐ€ ๊ฐ™์„๊ฒฝ์šฐ 0 ๋ฐ˜ํ™˜ / 1๋ฒˆ์งธ๊ฐ€ ๋” ํฌ๋ฉด 1๋ฐ˜ํ™˜ /2๋ฒˆ์งธ๊บผ๊ฐ€ ๋”ํฌ๋ฉด -1๋ฐ˜ํ™˜

m1 = "kingsman"
#=> "kingsman"
m2 = "ironman"
#=> "ironman"
m1 <=> m2
#=> 1
m3 = "ironman"
m2 <=> m3
#=> 0
m2 <=> m1
#=> -1

Blocks

๋ธ”๋ก์€ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ๋ฌถ์€ ๊ฒƒ์œผ๋กœ, ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ์‹œ ํ•œ๋ฒˆ๋งŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์ˆ˜์˜ ์ผ์ข…์ด๋‹ค.

nil

: one of two non-true value in Ruby (=>nothing at all) false ์™€ ๋‹ค๋ฅด๋‹ค.(=> not true)

conditional assignment operator ||=

using ||=, the hash key's value is only set once(์ฒ˜์Œ ์ง€์ •ํ•œ ๊ฒƒ์œผ๋กœ ์ง€์ •๋œ๋‹ค.) ์ฐธ์กฐํŽ˜์ด์ง€ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Yield

: yield๋ฅผ ์ด์šฉํ•ด์„œ method ์•ˆ์— ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์„ ๋‚˜์ค‘์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ. (You can also pass parameters to yield.)

def block_test
  puts "We're in the method!"
  puts "Yielding to the block..."
  yield
  puts "We're back in the method!"
end
block_test { puts ">>> We're in the block!" }
# => We're in the method!
    Yielding to the block...
    >>> We're in the block!
    We're back in the method!
    nil
def double(num)
  yield num
end
double(2) { |n| n*2 }
# => 4

Last updated