作者Esh ()
看板Ruby
标题Re: [问题] 如何再送参数到method?
时间Tue Mar 10 12:17:51 2009
换这个范例问可能比较单纯
input = ARGV[0]
def somemethod(value="#{input}")
puts value
end
somemethod()
输入ruby xxxx.rb 1111
想要印出1111,却会出现这样的错误讯息
show.rb:3:in `somemethod': undefined local variable or method `input' for
main:Object (NameError)
from show.rb:7
请问要怎麽解决?
谢谢
※ 引述《Esh ()》之铭言:
: 我写了一个程式有用到dbi mysql
: 主要是送入一个关键字作为where的条件
: 然後想要如果结果非nil
: 就再把这个结果送去当做where的条件
: 可是一直被说是参数错误
: 试了很多方法还是搞不定,所以想上来问一下高手
: require 'dbi'
: require 'mysql'
: def query()
: input = ARGV[0]
: dbh = Mysql.real_connect("localhost", "root", "", "g2p")
: q = dbh.query("select is_a from is_a where id='#{input}';")
: while row = q.fetch_row do
: mpid = row.to_s
: end
: return mpid
: end
: puts query()
: 如上面所示,目前这程式可以跑
: 我要把回传的结果mpid再当做input的值
: 请问该怎麽做才好 谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.129.162.64
1F:推 Schelfaniel:把 input 换用 global 的 $input 就好了 03/10 13:02