Answer the question
In order to leave comments, you need to log in
If a block is a closure, then why can't you access a local variable from it?
In this case, I can't access the name variable from the wrapper function:
def closure
name = "локальная"
puts "перед блоком"
yield(name) # name = yield(name)
puts "после блока"
puts name
end
closure do |word|
puts "Внутри блока."
word = word + " добавка от блока"
puts word
puts "Выход из блока."
# word
end
Answer the question
In order to leave comments, you need to log in
This is from not knowing that the + method creates a new object.
This is how you want it to be
closure do |word|
puts "Внутри блока."
word.concat " добавка от блока"
puts word
puts "Выход из блока."
# word
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question