看板 Ruby 關於我們 聯絡資訊
換這個範例問可能比較單純 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
Schelfaniel:把 input 換用 global 的 $input 就好了 03/10 13:02