Method
Method
Iterator
object.each { |item| # Do something }object.each do |item| # Do something endarray = [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]3.times { puts "hi" } # hi # hi # hi #=> 3a = "Hello, This is example" #=> "Hello, This is example" a.split(" ") #=> ["Hello,", "This", "is", "example"]:exam.to_s # ==> "exam" "exam".to_sym # ==> :examgrades = { 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}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[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.):hello.is_a? Symbol # ==> true
Methods(함수)
parameters and arguments
combined comparison operator <=>
<=>Blocks
nil
conditional assignment operator ||=
||=Yield
Last updated