W
W
weare1382014-08-17 16:33:26
ruby
weare138, 2014-08-17 16:33:26

How does the "yeld" method work?

Guys, explain on your fingers how this method works, otherwise I’m completely in a stupor

Answer the question

In order to leave comments, you need to log in

5 answer(s)
C
CAMOKPYT, 2014-08-17
@weare138

Calls the block passed to the method

def some_method(&block)
   yield
end

some_method { puts 'hello from Proc'}
# Напечатает  hello from Proc

Or with an argument
def some_method(agrument,&block)
   yield (agrument)
end

some_method('ITS ARGUMENT') {|argument| puts "hello from Proc with #{argument}"}
# Напечатает hello from Proc with ITS ARGUMENT

S
Sergey, 2014-08-17
Protko @Fesor

Are you talking about the "yield" keyword? Read about generators .

S
Sergey Krasnodemsky, 2014-08-17
@Prognosticator

On the fingers - By calling yield in the body of a method, we mean the following: transfer control to the block, and when it finishes, return control to this method .

D
Dmitry, 2014-08-17
@mayar

Here it is well described: nashbridges.me/blocks-in-ruby

T
Tortway, 2014-08-24
@Tortway

yield, as it were, teleports the code to its place

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question