Issue with Ruby variables -


i expect code insert 4 variables (which contain numerical characters) table in sqlite database:

on :"1" |m|   local      = m.params[1]   local_max  = m.params[2] end  on :"2" |m|   global     = m.params[1]   global_max = p m.params[2]   db.execute( "insert t (local, local_max, global, global_max) values(#{local}, #{local_max}, #{global}, #{global_max})" ) end 

but instead, generates errors:

> [2016/01/07 20:43:09.662] !! bot.rb:88:in `block (4 levels) in > <main>': undefined local variable or method `local' > #<cinch::callback:0x00000000c4fe00 @bot=#<bot nick="cinch">> (nameerror) 

the variables local , local_max exist in scope of first block (the first do...end).

"declaring" variables outside block , letting block capture them should enough:

local, local_max = nil, nil on :"1" |m|   local      = m.params[1]   local_max  = m.params[2] end  on :"2" |m|   global     = m.params[1]   global_max = p m.params[2]   db.execute( "insert t (local, local_max, global, global_max) values(#{local}, #{local_max}, #{global}, #{global_max})" ) end  

i (and else, seems) have no idea on is, it's worth noting if on fancy metaprogramming changes scoping of supplied block, might not work.


Comments

Popular posts from this blog

Capture and play voice with Asterisk ARI -

c++ - Can not find the "fiostream.h" file -

java - Why database contraints in HSQLDB are only checked during a commit when using transactions in Hibernate? -