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
Was this helpful?