Эх сурвалжийг харах

Fix the ngx_mruby db test so that it prints the world instead of "null" (#3057)

In the previous version, it was as if the "ret = ..." statement inside
the db.execute block was creating a *new* variable named ret rather than
reassigning the variable that was initialized to nil above, and so the
value of ret seen on the "Nginx.rputs ..." line was always nil.

Strangely (to me), wrapping the whole previous version in a function
definition ("def foo" ... "end") then calling that function ("foo()")
also results in the correct behavior.

I don't know why it behaves this way.  I don't think the implementation
of this test has changed in a long time, and it was fine in previous
rounds.
Michael Hixson 7 жил өмнө
parent
commit
cd1f7550a1

+ 4 - 6
frameworks/Ruby/ngx_mruby/db.rb

@@ -1,8 +1,6 @@
 # https://github.com/mattn/mruby-mysql/blob/master/example/example.rb
 db = Userdata.new("my_#{Process.pid}").db
-
-ret = nil
-db.execute("select * from World where id = ?", rand(10000)) do |row, fields|
-  ret = Hash[fields.zip(row)]
-end
-Nginx.rputs JSON::stringify(ret)
+result_set = db.execute("select * from World where id = ?", rand(10000))
+world = Hash[result_set.fields.zip(result_set.next)]
+result_set.close
+Nginx.rputs JSON::stringify(world)