Ruby/Method/Code Block

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

0.step(50, 5) { ...code to loop here... }

0.step(50, 5) { puts "Test" }



10.downto(5) { ...code to loop here... }

10.downto(5) { puts "Test" }



1.upto(5) { ...code to loop here... }

1.upto(5) { puts "Test" }



1.upto(5) { |number| puts number }

1.upto(5) do |number|
  puts number
end



A block is a section of code that can be passed to a method much like passing an argument.

A block is delimited with {}:
{ puts "Hello there!" }
or do and end:
do
  puts "Hello there!"
end
# Generally, you use {} for a one-line block, and do..end for a multi-line block.



A code block is an anonymous, nameless method or function.

# This code is passed to the each method that then runs the code block for each element of the array.
# You can write methods of your own to handle code blocks.
def each_vowel(&code_block)
  %w{a e i o u}.each { |vowel| code_block.call(vowel) }
end
each_vowel { |vowel| puts vowel }



block is invoked in conjunction with a method.

# block is referred to as a nameless function.
# block in Ruby is often an idiom for getting all the values out of a data structure. 
pacific = [ "Washington", "Oregon", "California" ]
pacific.each do |element|
 puts element
end
# characters (|element|) can be any name you want. 
# The block uses it as a local variable to keep track of every element in the array
# later uses it to do something with the element. 
# This block uses puts to print each element in the array:
# You can replace do/end with a pair of braces: pacific.each { |e| puts e }



block pointer

def my_lambda(&block)
  block
end
b = my_lambda { puts "Hello World My Way!" }
b.call
# Hello World My Way!
puts b.class                                # => Proc



Code Blocks and each method

x = [1, 2, 3]
x.each { |y| puts y }
# The each method accepts a single code block as a parameter. 
# The code block is defined within the { and } symbols, or within do and end delimiters:
x = [1, 2, 3]
x.each do |y|
  puts y
end



convert a block into an object.

# This object is called a proc (procedure). 
# procs preserve their execution environment and pack it along with them. 
# The lambda method is one way to create a proc object. 
 
prc = lambda { |name| puts "Hello, " + name }
prc.call "Matz!" # => Hello, Matz!



define hello so that it contains only a yield statement,

then call the new version of hello with a block (the code in braces).
def hello
  yield
end
hello { puts "Hello, Matz!" } # => Hello, Matz!



Nested block logics

#!/usr/bin/env ruby
def times_tables
  1.upto(12) { |i| 1.upto(12) { |j| print i.to_s + " x " + j.to_s + " = ", j * i, "\n"} }
end
times_tables



Place a space between the pipes ( | ) and the variable names

5.times do | i |
  puts i
end



print all the elements in an array using the each method followed by a block

[ "Hello, ", "Matz!"].each { |e| print e }



Running a Code Block Periodically

def every_n_seconds(n)
  loop do
    before = Time.now
    yield
    interval = n-(Time.now-before)
    sleep(interval) if interval > 0
  end
end
every_n_seconds(5) do
 puts "#{Time.now.strftime("%X")}... beep!"
end



use code blocks with Ruby iterators like each, upto, downto, and times

["Hello", "there", "sweetie."].each {|word| puts word}
4.upto(8) {|loop_index_value| puts loop_index_value}
4.times {print "X"}