在Shoes中使用实例变量的奇怪行为

时间:2011-04-20 21:38:39

标签: ruby shoes

嘿,大家好。我正在使用Shoes为Ruby项目制作GUI。

我有一个名为Manager的类(在内存管理器中),它从文件中加载一个'进程列表',将其拆分并在进行某个执行调用时将内容分配给内存中的不同“页面”。不过,我真的不认为这部分太重要了。这一切都可以作为终端应用程序使用。

然而,鞋子让我很困惑。这是我到目前为止所得到的:

Shoes.app(:title => "Paging Simulator", :width => 800, :height => 450) do
  @manager = Manager.new
  stack(:width => 200) do
    @exec_list = stack {
      title "Execution Queue", :size => 14
      @exec_lines = para "click button to load", :size => 9
      @file_button = button "Load Process List"
      @file_button.click {
        filename = ask_open_file
        # @manager.set_exec_list filename
        # alert "this makes no sense"
        @exec_lines.text = @manager.exec_list.join "\n"
        # exec_lines.text = File.read filename
      }
    }
  end
end

运行时会发生什么:

程序视图按预期加载。我得到一个标题,一个段落,上面写着“点击按钮......”,还有一个按钮。我单击按钮,然后选择文件。但这就是事情变得怪异的地方。

如果我按照我的意愿运行最后一个注释行exec_lines.text = File.read filename,但我的经理没有获得所需的任何信息。

如果我运行@manager.set_exec_list filename行,则阻止该行中的 运行,包括alert或我尝试放入的任何其他代码

如果我按上面所示运行,但是,我得到了我期望的输出,但是我无法从我选择的文件中设置数据。

我试图在Shoes Rules页面上解决这个问题,但这似乎不是他们解决的问题,而且“它改变/不会改变自我”我认为我掌握了,但这令人困惑,我不认为这与这个问题完全相关。

有没有人知道如何让它发挥作用?我对这个项目有点紧张的时间,我似乎无法获得任何其他Ruby GUI工具包甚至运行,所以我觉得我很不满鞋子。

感谢。

更新 当我调用@manager.set_exec_list filename时,我已经尝试在代码上运行ruby-debug,并且单步执行它表明已经进行了此调用,但代码实际上从未实际(从我可以看出)跳转到该方法,就像它是块中的最后一行代码一样。我是否需要在Shoes.app块中包含这些类? 更新不。这没什么不同。

更新完整源代码如下:

#!/usr/bin/env shoes
require 'rubygems'
require 'ruby-debug'

class MemSegment
  attr_accessor :filled, :pid, :seg, :seg_id

  def initialize(filled=false, pid=nil, seg=nil, seg_id=0)
    @filled = filled
    @pid = pid.to_i
    @seg = seg.to_s
    @seg_id = seg_id.to_i
    self
  end

  def fill(pid, seg, seg_id)
    @filled = true; @pid = pid; @seg = seg; @seg_id = seg_id;
    self
  end

  def clear
    self.filled = false; self.pid = nil; self.seg = nil;
    self
  end

  def filled?
    @filled
  end

  def to_s
    filled? ? "#{seg} #{seg_id} for pid #{pid}" : "Free"
  end
end

class SimProc
  include Enumerable
  attr_accessor :pid, :code, :data

  def initialize(pid, code, data)
    @pid = pid.to_i
    @code = code.to_i
    @data = data.to_i
  end

  def each
    yield :code, code
    yield :data, data
  end

  def to_s
    "[SimProc :pid => #{pid}, :code => #{code}, :data => #{data}]"
  end

  def to_a
    [@pid, @code, @data]
  end
end

class Manager
  attr_reader :segments, :processes, :exec_list, :exec_object

  def initialize
    @exec_list = [[1, 2], [3, 4], [5, 6]]
    @processes = {}
    @segments = Array.new(8) { MemSegment.new }
  end

  def print_activity
    @segments.each_with_index {|s, index| puts "Seg #{index} => #{s}" }
    @processes.each_value {|s| puts s }
  end

  def load_process(pcb, exec_index)
    if pcb.size == 3
      p = SimProc.new(*pcb)
      bad_load = false

      @processes.store p.pid, p
      @processes[p.pid].each do |proc_seg, bsize|
        (bsize / 512.0).ceil.times do |seg_id|
          @segments.each_with_index do |s, index|
            if !s.filled
              #find the first empty memory segment
              s.fill p.pid, proc_seg, seg_id
              break
            # if all slots are filled and we couldn't place a proc block
            elsif index == @segments.size - 1
              bad_load = true
              puts "Cannot find a place for #{proc_seg} segment of size #{bsize}. Requeueing..."
              break;
            end
          end
          break if bad_load
        end
      end
      # recover pages and queue the process for later
      if bad_load
        @segments.each_with_index do |seg, seg_index|
          # clear any segments that didn't get loaded properly
          if seg.pid == p.pid
            seg.clear
            puts "Seg #{seg_index} => segment cleared: #{seg}"
          end
        end
        # reinsert this process after the next in the execution list
        # it will attempt to load and run after the next process is performed
        @exec_list.insert(exec_index + 2, p.to_a)
      end
      print_activity

    elsif pcb.size == 2 and pcb[1] == -1
      # a process is exiting
      puts "removing pid #{pcb[0]}"
      @segments.each { |s| s.clear if s.pid == pcb[0] }
      @processes.delete pcb[0]
      print_activity
    end
  end

  def set_exec_list(filename)
    file = File.open filename
    file.each { |pcb| @exec_list << pcb.split.map(&:to_i) } unless file.nil?
    filename
  end

  def main
    exseq = File.open('exseq2.txt')
    set_exec_list exseq

    # this is the object that will be used to run each process with .next
    @exec_object = @exec_list.each_with_index
    # @exec_list.each_with_index { |pcb, exec_index| load_process(pcb, exec_index) }
    (@exec_list.size + 1).times do
      load_process(*@exec_object.next)
    end
  end
end

=begin
manager = Manager.new
manager.main
=end

#=begin
Shoes.app(:title => "Paging Simulator", :width => 800, :height => 450) do
  @manager = Manager.new
  stack(:width => 200) do
    @exec_list = stack {
      title "Execution Queue", :size => 14
      @exec_lines = para "click button to load", :size => 9
      @file_button = button "Load Process List"
      debugger
      @file_button.click {
        filename = ask_open_file
        @manager.set_exec_list filename
        # alert "this makes no sense"
        # @exec_lines.text = @manager.exec_list
        # @exec_lines.text = File.read filename
        @exec_lines.text = @manager.exec_list.join "\n"
      }
    }
  end
end
#=end

1 个答案:

答案 0 :(得分:3)

所以,有一些事情:

#1,我没有Manager的实现,所以我不能告诉你它为什么会中断。您是否尝试检查Shoes控制台是否有任何错误?命中控制 - /将其提升。如果'在击中该线之后没有任何运行',那可能就是问题。

#2,只要您在最后一行将exec_lines更改为@exec_lines,这对我有用。这是我试过的:

class Manager;end
Shoes.app(:title => "Paging Simulator", :width => 800, :height => 450) do
  @manager = Manager.new
  stack(:width => 200) do
    @exec_list = stack {
      title "Execution Queue", :size => 14
      @exec_lines = para "click button to load", :size => 9
      @file_button = button "Load Process List"
      @file_button.click {
        filename = ask_open_file
        #alert "this makes no sense"
        @exec_lines.text = File.read filename
      }
    }
  end
end

希望有所帮助!