Ruby / Shoes无法从list_box的选择中获取数值

时间:2013-07-21 15:15:55

标签: ruby conditional shoes

我试图根据list_box上的选择从条件语句中获取一个整数。只是不能使它工作。另外,我希望能够删除@acuity = para。我的代码基于:list_box won't use default value on startup - Shoes.

Shoes.app :title=> 'Procedural', :width => 300, :height => 200 do 

@list_box = list_box :items => ["1- Ad lib", "2- Assist of SB", "3- Assist of 1", "4- Assist of 2"]                        

   button 'Compute' do
        @acuity.text = @list_box.text
  if @acuity == "1- Ad lib"
        @mobility = 11
  elsif @acuity == "2- Assist of SB"
        @mobility = 22
  elsif @acuity == "3- Assist of 1"
    @mobility = 33
  elsif @acuity == "4- Assist of 2"
    @mobility = 44
  end

      end

    @acuity = para

    end

1 个答案:

答案 0 :(得分:2)

目前尚不清楚你的目标是什么。在某处显示@mobility?只需为@mobility变量赋值就什么都不做。

Shoes.app do

  @choice_map = {
    "1- Ad lib"       => 11,
    "2- Assist of SB" => 22,
    "3- Assist of 1"  => 33,
    "4- Assist of 2"  => 44
  }

  para "Choose item:"
  @my_listbox = list_box(:items => @choice_map.keys) 

  button 'Compute' do
    @mobility = @choice_map[@my_listbox.text]
  end

end

要证明@mobility包含正确的值,您需要在某处显示值:

Shoes.app do

  @choice_map = {
    "1- Ad lib"       => 11,
    "2- Assist of SB" => 22,
    "3- Assist of 1"  => 33,
    "4- Assist of 2"  => 44
  }

  @my_listbox = list_box(items: @choice_map.keys)

  @my_para = para 'show computed results here'

  button 'Compute' do
    @mobility = @choice_map[@my_listbox.text]
    @my_para.text = @mobility
  end

end

您的代码实际上可以正常工作,但稍作修改:

Shoes.app :title => 'Procedural', :width => 300, :height => 200 do 

@list_box = list_box :items => ["1- Ad lib", "2- Assist of SB", "3- Assist of 1", "4- Assist of 2"]                        

@my_para = para 'show computed results here'

button 'Compute' do
  @acuity = @list_box.text   #<---CHANGE HERE

  if @acuity == "1- Ad lib"
    @mobility = 11
  elsif @acuity == "2- Assist of SB"
    @mobility = 22
  elsif @acuity == "3- Assist of 1"
    @mobility = 33
  elsif @acuity == "4- Assist of 2"
    @mobility = 44
  end

  @my_para.text = @mobility

end

如果您不需要按钮:

Shoes.app do

  @choice_map = {
    "1- Ad lib"       => 11,
    "2- Assist of SB" => 22,
    "3- Assist of 1"  => 33,
    "4- Assist of 2"  => 44
  }

  para "Choose item:"

  @default = "2- Assist of SB"
  @mobility = @choice_map[@default]

  @my_listbox = list_box(items: @choice_map.keys, choose: @default) do |list|
    @mobility = @choice_map[list.text]
  end

end

给list_box()的块只在onchange上执行,所以你必须为@mobility设置一个初始值。

您会认为以下内容可以检索列表框中的初始值:

Shoes.app do

  @choice_map = {
    "1- Ad lib"       => 11,
    "2- Assist of SB" => 22,
    "3- Assist of 1"  => 33,
    "4- Assist of 2"  => 44
  }

  para "Choose item:"

  @my_listbox = list_box(items: @choice_map.keys) do |list|
    @mobility = @choice_map[list.text]
    @my_para.text = @mobility
  end

  @mobility = @choice_map[@my_listbox.text]
  @my_para = para @mobility 
end

但显然列表框在app块执行完毕后才存在,因此你无法在app块中检索@ my_listbox.text。