What does this ruby code mean?
recently in the study of elk, need to embed ruby code, but also hope to understand ruby to help, explain, thank you!
just explain what each of the following two parts represents.
//
xx "xxx" do
end
//
xx("xxx") do |xx|
end
the original code is as follows
test "drop percentage 100% " do
parameters do
{"percentage"=>1}
end
in_event {{"message"=>"bonjour bonjour","date"=>"2018-2-2"}}
expect("drops the event") do |events|
puts "-------->expect"
events.size == 0
end
end
this code is a unit test, explained roughly as follows:
parameters do
{"percentage"=>1}
end
The
code means that parameters is a function with no arguments, and this function can perform operations with a code block, that is, the do.end part, embedded in the yield part of the function. For example, an assignment operation is given to the parameters function.
how do I use this yield? Take a look at the following example:
-sharp test
def test
puts '1'
yield -sharp
puts '3'
end
test do
puts '2' -sharp
end
then output 1, 2, 3
same thing,
expect("drops the event") do |events|
puts "-------->expect"
events.size == 0
end
here is a function with parameters, followed by a code block. The function expect is used to make a test assertion. The parameter "drops the event" is the description of this assertion-- hoping to discard all events, and then the function can give a parameter to the yield. Let the code block use yield (events),. For example, take out the events here, and the code block determines that the events.size is 0.
The
expect function is roughly defined like this:
def expect(describe)
events = [] -sharp events
result = yield(events) -sharp events
if result == true
puts ''
else
puts ''
end
end
when you look at the specific use, the code block determines that events.size = = 0, and then the result true is assigned to the result,expect function in the expect function to continue to execute.
in addition, the code block is not just do.end and do | unit |. End, here {.} after in_event is also a code block, the content of the code block is {"message" = > "bonjour bonjour", "date" = > "2018-2-2"} this hash, and the use of a code block is {| unit |.}. That is, you can think of the opening curly braces as do, and the closing curly braces as end.
I hope I can help you.