如何从Ruby中的另一个实例访问全局实例变量

时间:2022-09-06 13:51:28

I'm new to Ruby and I'm trying to change some of my code to have OO design using our current framework. When converting my utility file to classes I encountered the following problem:

我是Ruby的新手,我正在尝试使用我们当前的框架来改变我的一些代码来进行OO设计。将我的实用程序文件转换为类时遇到以下问题:

There are two functions *create_output_file* and *write_to* are framework methods (don't belong to any classes) that I cannot touch (They specifies where to write to the framework).
And there is a class A I wrote which needs to use the *write_to* to log the time and return status of the foo().

有两个函数* create_output_file *和* write_to *是我无法触及的框架方法(不属于任何类)(它们指定写入框架的位置)。还有一个我编写的类A需要使用* write_to *来记录foo()的时间和返回状态。

<!-- language: lang-rb -->
def create_output_file(params)
  @output_file = params["SS_output_file"]
  @hand = File.open(@output_file, "a")
  write_to "New Run - Shell Cmd\n\n"
  return true
end
def write_to(message, newline = true)
  return if message.nil?
  sep = newline ? "\n" : ""
  @hand.print(message + sep)
  print(message + sep)
end

def this_is_dum
  print("This is to show class A have access to global methods.\n")
end

class A
  def foo()
    # Do something that needs the status of instance A
    this_is_dum()
    write_to("This is the current status!")
  end

end

# Main routine
params = {}
params["SS_output_file"] = "./debugging_log"
create_output_file(params)  #sets the @hand file handle
A.new.foo

These are the outputs I'm getting:

这些是我得到的输出:

New Run - Shell Cmd:

新运行 - Shell Cmd:

This is to show class A have access to global methods. D:/data/weshi/workspace/RubyBRPM2/scripts/Random.rb:12:in `write_to': private method `print' called for nil:NilClass (NoMethodError)
from D:/data/weshi/workspace/RubyBRPM2/scripts/Random.rb:18:in `foo'
from D:/data/weshi/workspace/RubyBRPM2/scripts/Random.rb:26:in `'

这是为了表明A类可以访问全局方法。 D:/data/weshi/workspace/RubyBRPM2/scripts/Random.rb:12:在`write_to'中:来自D的私有方法`print'调用nil:NilClass(NoMethodError):/ data / weshi / workspace / RubyBRPM2 / scripts /Random.rb:18:来自D:/data/weshi/workspace/RubyBRPM2/scripts/Random.rb:26的'foo':''

After digging a bit I found that @hand was not accessed during the foo() scope.
I'm not sure how I'll be able to access the @hand variable inside class A and what it means without assigning to a class. But I do need the write_to function to work in accordance to the whole framework. Any suggestions or instructions are welcomed and appreciated.

挖了一下之后,我发现在foo()范围内没有访问@hand。我不确定如何能够访问A类中的@hand变量以及它在没有分配给类的情况下意味着什么。但我确实需要write_to函数按照整个框架工作。任何建议或指示都受到欢迎和赞赏。

1 个解决方案

#1


0  

You can give @hand to your A instance in the constructor like that:

您可以在构造函数中将@hand提供给您的A实例:

def create_output_file(params)
  @output_file = params["SS_output_file"]
  @hand = File.open(@output_file, "a")
  write_to "New Run - Shell Cmd\n\n"
  return true
end
def write_to(message, newline = true)
  return if message.nil?
  sep = newline ? "\n" : ""
  @hand.print(message + sep)
  print(message + sep)
end

def this_is_dum
  print("This is to show class A have access to global methods.\n")
end

class A
  def initialize(hand)
    @hand = hand
  end

  def foo()
    # Do something that needs the status of instance A
    this_is_dum()
    write_to("This is the current status!")
  end

end

# Main routine
params = {}
params["SS_output_file"] = "./debugging_log"
create_output_file(params)  #sets the @hand file handle
A.new(@hand).foo

The output is:

输出是:

New Run - Shell Cmd

This is to show class A have access to global methods.
This is the current status!

Otherwise I found that which may be a little bit more complicated.

否则我发现可能会有点复杂。

#1


0  

You can give @hand to your A instance in the constructor like that:

您可以在构造函数中将@hand提供给您的A实例:

def create_output_file(params)
  @output_file = params["SS_output_file"]
  @hand = File.open(@output_file, "a")
  write_to "New Run - Shell Cmd\n\n"
  return true
end
def write_to(message, newline = true)
  return if message.nil?
  sep = newline ? "\n" : ""
  @hand.print(message + sep)
  print(message + sep)
end

def this_is_dum
  print("This is to show class A have access to global methods.\n")
end

class A
  def initialize(hand)
    @hand = hand
  end

  def foo()
    # Do something that needs the status of instance A
    this_is_dum()
    write_to("This is the current status!")
  end

end

# Main routine
params = {}
params["SS_output_file"] = "./debugging_log"
create_output_file(params)  #sets the @hand file handle
A.new(@hand).foo

The output is:

输出是:

New Run - Shell Cmd

This is to show class A have access to global methods.
This is the current status!

Otherwise I found that which may be a little bit more complicated.

否则我发现可能会有点复杂。