Home>
I'm sorry for lack of study.
I couldn't understand it with my head. . .
I don't know why this Proc instance holds a value and can increment each time I call
def get_counter (start)
Proc.new {| n | start + = n}
end
count_up = get_counter (1)
count_up.call (1) # =>2
count_up.call (1) # =>3
count_up.call (1) # =>4
Thinking
(1)
count_up = get_counter (1)
# At this time, Proc instance assigned to variable count_up
Proc.new {| n | 1 + = n}
# 1 = 1 + n isn't strange ...?
# I thought that it would be preserved that it was executed in Proc, I tried it
count_up = Proc.new {| n | 1 + n}
count_up.call (1) # =>2
count_up.call (1) # =>2
(2) If this is the case, I understand the meaning ...
start = 1
count_up = Proc.new {| n | start + = n}
count_up.call (1) # =>2
count_up.call (1) # =>3
-
Answer # 1
Related articles
- ruby - i want to get the minimum and average values for the total value of a group of rails data
- ruby - how to pass values from rails controller to view
- ruby - behavior that i do not know if rails can be started with unicorn
- ruby - is it possible to apply column values in model validation (character limit)?
- ruby on rails - i want to sort the values of the graph created by gem "chartkick"
- ruby - i want to retrieve only the values of a specific column from the rails array
- ruby - i want to output all values at the beginning in the list search
- ruby - i want to make a graph of the values retrieved by the each statement with chart kick
- i want to convert hash values in ruby under specific conditions
- ruby - i don't know how to retrieve the values of the array in the intermediate table
- ruby - i want to get the column values of other models
- ruby - how to sort the values of rails variables in ascending order
- scraped values in ruby are stored in hash and made into an array
- ruby - model name interpretation when data is assigned to two instances
- [ruby on rails] i want to use multiple values with the sorting function using ransack
- ruby - i can't save values to the database with rails c
Trends
"Constant
1
" is different from "Variable whose initial value is1
".Yes. It is the same mechanism.