グローバル変数を使いすぎだったのでメンバに変更しました
普通に$を消すとうまいこと動作しなかったんですが
それローカル変数ですねw
classを作ってそのメンバにして委譲したら上手くいきました
メンバなのでGCの対象にもなりそう
追記b:変数の名前をリファクタしました
追記c:デフォルトキーを設定しました
:改行出力の箇所を変更しました
# encoding: UTF-8
# 以下はサンプルプログラム
# SCENARIO_TEST_01c_R02.rb
# 追記b:変数の名前をリファクタしました
# 追記c:デフォルトキーを設定しました
# :改行出力の箇所を変更しました
# Ruby動作2.6で確認
require 'io/console'
class Sys_Vars
# ボタンが押された時にこれらの変数をselected_btnextに代入して
# メインルーチンにジャンプする
attr_accessor :next_1
attr_accessor :next_2
# これがtrueになると終了する
attr_accessor :exit
# ボタンを押して選択される選択肢のID
attr_accessor :selected_next
def initialize()
@next_1 = "a"
@next_2 = "a"
@exit = false
@selected_next = "a"
@def_push_key = "z"
end
def set_def_push_key arg
@def_push_key = arg
end
def get_def_push_key arg = nil
if arg == nil
return @def_push_key
end
return arg
end
end
# ここからメインルーチン
class Main
def initialize()
@sv = Sys_Vars.new()
end
def run_main
tmp_ch = "?"
while 1
if @sv.exit == true then break end
case@sv.selected_next
when "a" then
@sv.next_1 = "aa"
@sv.next_2 = "ab"
print("zキーかxキーを押してください\n")
print("終了:qキー")
while 1
tmp_ch = get_getc()
if tmp_ch == "z"
@sv.selected_next = @sv.next_1
print("\n\n")
break
elsif tmp_ch == "x"
@sv.selected_next = @sv.next_2
print("\n\n")
break
elsif tmp_ch == @sv.get_def_push_key("q")
@sv.exit = true
print("\n\n")
print("QUIT")
tmp_ch = get_getc()
break
end
end # end of while B
when "aa" then
@sv.next_1 = "a"
print("あなたはzキーを押しましたね\n")
print("NEXT:zキー")
while 1
tmp_ch = get_getc()
if tmp_ch == @sv.get_def_push_key
@sv.selected_next = @sv.next_1
print("\n\n")
break
end
end # end of while C
when "ab" then
@sv.next_1 = "a"
print("あなたはxキーを押しましたね\n")
print("NEXT:zキー")
while 1
tmp_ch = get_getc()
if tmp_ch == @sv.get_def_push_key
@sv.selected_next = @sv.next_1
print("\n\n")
break
end
end # end of while D
end # end of case
end # end of while A
end # end of method
end # end of class
def get_getc
$stdin.raw do |io|
ch = io.getc
print ch
return ch
end
end
m=Main.new()
m.run_main