Ruby 什么';是一种跨对象调用方法的技术,使用;“发送”;用红宝石?

Ruby 什么';是一种跨对象调用方法的技术,使用;“发送”;用红宝石?,ruby,Ruby,我正在做一些涉及Ruby中异步回调的编程,需要将回调方法传递给另一个对象(可以静态调用也可以不静态调用)。我遇到的问题是实例回调的语法——我知道这相当复杂,但我不确定是否可以简化。以下是我所拥有的: class OBJA def self.staticMethod(text, returnCall) puts "objA.staticMethod: #{text}" OBJB.send(returnCall, "Call back from objA.staticMethod

我正在做一些涉及Ruby中异步回调的编程,需要将回调方法传递给另一个对象(可以静态调用也可以不静态调用)。我遇到的问题是实例回调的语法——我知道这相当复杂,但我不确定是否可以简化。以下是我所拥有的:

class OBJA
  def self.staticMethod(text, returnCall)
    puts "objA.staticMethod: #{text}"
    OBJB.send(returnCall, "Call back from objA.staticMethod")
  end

  def instanceMethod(text, returnCall)
    puts "objA.instanceMethod: #{text}"
    OBJB.send(returnCall, "Call back from objA.instanceMethod")
  end
end

class OBJB
  def starterMethod
    OBJA.staticMethod("foo", :returnedCall)
    OBJA.new.instanceMethod("bar", :returnedCall)
  end

  def returnedCall(text)
    puts text
  end
end
您可以通过执行以下操作来执行它:

b = OBJB.new
b.starterMethod
谢谢

我遇到的问题是实例回调的语法

必须使用实例调用实例方法。如果在类上调用类方法,例如,
OBJB.send(…)
,则必须定义类方法

class OBJA
  def self.staticMethod(text, methName)
    puts "objA.staticMethod: #{text}"
    OBJB.send(methName, "Call back from objA.staticMethod")
  end

  def instanceMethod(text, methName)
    puts "objA.instanceMethod: #{text}"
    OBJB.new.send(methName, "Call back from objA.instanceMethod")
  end
end


class OBJB
  def starterMethod
    OBJA.staticMethod("foo", :returnedCall)
    OBJA.new.instanceMethod("bar", :returnedCall)
  end

  def self.returnedCall(text)
    puts text
  end

  def returnedCall(text)
    puts text
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
Call back from objA.staticMethod
objA.instanceMethod: bar
Call back from objA.instanceMethod
还可以将块传递给OBJA方法:

class OBJA
  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    block.call("Call back from objA.staticMethod")
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call("Call back from objA.instanceMethod")
  end
end


class OBJB
  def starterMethod
    OBJA.staticMethod("foo") {|str| puts str}
    OBJA.new.instanceMethod("bar") {|str| puts str}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
Call back from objA.staticMethod
objA.instanceMethod: bar
Call back from objA.instanceMethod
或者,更具说明性的结束:

class OBJA
  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    block.call
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call
  end
end


class OBJB

  def initialize
    @x = 1
    @y = 2
  end

  def starterMethod
    OBJA.staticMethod("foo") {puts instance_variable_get(:@x)}
    OBJA.new.instanceMethod("bar") {puts instance_variable_get(:@y)}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
1
objA.instanceMethod: bar
2
我不知道这是否对你有帮助,但是这个技巧在Ruby框架中被广泛使用。Ruby是编程语言的狂野西部,它实际上允许您忽略闭包。当您希望从某些代码中接受块,但不希望在定义块的上下文中执行块时,这非常有用——相反,您希望在创建的上下文中执行块

class OBJA
    @x = 10  #Instance variables attach themselves to whatever object is
    @y = 20  #self at the time they are created.  Inside a class, but outside
             #any defs, self is equal to the class, so these statements
             #create  what are known as 'class instance variables' (@@variables
             #aren't used in ruby because they don't behave 'correctly').


  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    instance_eval &block  #See comment (1) below
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call
  end
end


class OBJB

  def initialize
    @x = 1
    @y = 2
  end

  def starterMethod
    OBJA.staticMethod("foo") {puts instance_variable_get(:@x)}
    OBJA.new.instanceMethod("bar") {puts instance_variable_get(:@y)}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
10  #<--CHECK THIS OUT!!
objA.instanceMethod: bar
2
实例_eval()用于将自变量的值更改为接收器。但self已经和OBJA一样了??instance_eval()成功完成的是更改其块看到的自变量的值!通过将块变量转换为instance_eval的块,实际上可以更改块上下文,即块代码看到的变量

我遇到的问题是实例回调的语法

必须使用实例调用实例方法。如果在类上调用类方法,例如,
OBJB.send(…)
,则必须定义类方法

class OBJA
  def self.staticMethod(text, methName)
    puts "objA.staticMethod: #{text}"
    OBJB.send(methName, "Call back from objA.staticMethod")
  end

  def instanceMethod(text, methName)
    puts "objA.instanceMethod: #{text}"
    OBJB.new.send(methName, "Call back from objA.instanceMethod")
  end
end


class OBJB
  def starterMethod
    OBJA.staticMethod("foo", :returnedCall)
    OBJA.new.instanceMethod("bar", :returnedCall)
  end

  def self.returnedCall(text)
    puts text
  end

  def returnedCall(text)
    puts text
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
Call back from objA.staticMethod
objA.instanceMethod: bar
Call back from objA.instanceMethod
还可以将块传递给OBJA方法:

class OBJA
  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    block.call("Call back from objA.staticMethod")
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call("Call back from objA.instanceMethod")
  end
end


class OBJB
  def starterMethod
    OBJA.staticMethod("foo") {|str| puts str}
    OBJA.new.instanceMethod("bar") {|str| puts str}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
Call back from objA.staticMethod
objA.instanceMethod: bar
Call back from objA.instanceMethod
或者,更具说明性的结束:

class OBJA
  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    block.call
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call
  end
end


class OBJB

  def initialize
    @x = 1
    @y = 2
  end

  def starterMethod
    OBJA.staticMethod("foo") {puts instance_variable_get(:@x)}
    OBJA.new.instanceMethod("bar") {puts instance_variable_get(:@y)}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
1
objA.instanceMethod: bar
2
我不知道这是否对你有帮助,但是这个技巧在Ruby框架中被广泛使用。Ruby是编程语言的狂野西部,它实际上允许您忽略闭包。当您希望从某些代码中接受块,但不希望在定义块的上下文中执行块时,这非常有用——相反,您希望在创建的上下文中执行块

class OBJA
    @x = 10  #Instance variables attach themselves to whatever object is
    @y = 20  #self at the time they are created.  Inside a class, but outside
             #any defs, self is equal to the class, so these statements
             #create  what are known as 'class instance variables' (@@variables
             #aren't used in ruby because they don't behave 'correctly').


  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    instance_eval &block  #See comment (1) below
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call
  end
end


class OBJB

  def initialize
    @x = 1
    @y = 2
  end

  def starterMethod
    OBJA.staticMethod("foo") {puts instance_variable_get(:@x)}
    OBJA.new.instanceMethod("bar") {puts instance_variable_get(:@y)}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
10  #<--CHECK THIS OUT!!
objA.instanceMethod: bar
2
实例_eval()用于将自变量的值更改为接收器。但self已经和OBJA一样了??instance_eval()成功完成的是更改其块看到的自变量的值!通过将块变量转换为instance_eval的块,实际上可以更改块上下文,即块代码看到的变量

我遇到的问题是实例回调的语法

必须使用实例调用实例方法。如果在类上调用类方法,例如,
OBJB.send(…)
,则必须定义类方法

class OBJA
  def self.staticMethod(text, methName)
    puts "objA.staticMethod: #{text}"
    OBJB.send(methName, "Call back from objA.staticMethod")
  end

  def instanceMethod(text, methName)
    puts "objA.instanceMethod: #{text}"
    OBJB.new.send(methName, "Call back from objA.instanceMethod")
  end
end


class OBJB
  def starterMethod
    OBJA.staticMethod("foo", :returnedCall)
    OBJA.new.instanceMethod("bar", :returnedCall)
  end

  def self.returnedCall(text)
    puts text
  end

  def returnedCall(text)
    puts text
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
Call back from objA.staticMethod
objA.instanceMethod: bar
Call back from objA.instanceMethod
还可以将块传递给OBJA方法:

class OBJA
  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    block.call("Call back from objA.staticMethod")
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call("Call back from objA.instanceMethod")
  end
end


class OBJB
  def starterMethod
    OBJA.staticMethod("foo") {|str| puts str}
    OBJA.new.instanceMethod("bar") {|str| puts str}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
Call back from objA.staticMethod
objA.instanceMethod: bar
Call back from objA.instanceMethod
或者,更具说明性的结束:

class OBJA
  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    block.call
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call
  end
end


class OBJB

  def initialize
    @x = 1
    @y = 2
  end

  def starterMethod
    OBJA.staticMethod("foo") {puts instance_variable_get(:@x)}
    OBJA.new.instanceMethod("bar") {puts instance_variable_get(:@y)}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
1
objA.instanceMethod: bar
2
我不知道这是否对你有帮助,但是这个技巧在Ruby框架中被广泛使用。Ruby是编程语言的狂野西部,它实际上允许您忽略闭包。当您希望从某些代码中接受块,但不希望在定义块的上下文中执行块时,这非常有用——相反,您希望在创建的上下文中执行块

class OBJA
    @x = 10  #Instance variables attach themselves to whatever object is
    @y = 20  #self at the time they are created.  Inside a class, but outside
             #any defs, self is equal to the class, so these statements
             #create  what are known as 'class instance variables' (@@variables
             #aren't used in ruby because they don't behave 'correctly').


  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    instance_eval &block  #See comment (1) below
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call
  end
end


class OBJB

  def initialize
    @x = 1
    @y = 2
  end

  def starterMethod
    OBJA.staticMethod("foo") {puts instance_variable_get(:@x)}
    OBJA.new.instanceMethod("bar") {puts instance_variable_get(:@y)}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
10  #<--CHECK THIS OUT!!
objA.instanceMethod: bar
2
实例_eval()用于将自变量的值更改为接收器。但self已经和OBJA一样了??instance_eval()成功完成的是更改其块看到的自变量的值!通过将块变量转换为instance_eval的块,实际上可以更改块上下文,即块代码看到的变量

我遇到的问题是实例回调的语法

必须使用实例调用实例方法。如果在类上调用类方法,例如,
OBJB.send(…)
,则必须定义类方法

class OBJA
  def self.staticMethod(text, methName)
    puts "objA.staticMethod: #{text}"
    OBJB.send(methName, "Call back from objA.staticMethod")
  end

  def instanceMethod(text, methName)
    puts "objA.instanceMethod: #{text}"
    OBJB.new.send(methName, "Call back from objA.instanceMethod")
  end
end


class OBJB
  def starterMethod
    OBJA.staticMethod("foo", :returnedCall)
    OBJA.new.instanceMethod("bar", :returnedCall)
  end

  def self.returnedCall(text)
    puts text
  end

  def returnedCall(text)
    puts text
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
Call back from objA.staticMethod
objA.instanceMethod: bar
Call back from objA.instanceMethod
还可以将块传递给OBJA方法:

class OBJA
  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    block.call("Call back from objA.staticMethod")
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call("Call back from objA.instanceMethod")
  end
end


class OBJB
  def starterMethod
    OBJA.staticMethod("foo") {|str| puts str}
    OBJA.new.instanceMethod("bar") {|str| puts str}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
Call back from objA.staticMethod
objA.instanceMethod: bar
Call back from objA.instanceMethod
或者,更具说明性的结束:

class OBJA
  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    block.call
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call
  end
end


class OBJB

  def initialize
    @x = 1
    @y = 2
  end

  def starterMethod
    OBJA.staticMethod("foo") {puts instance_variable_get(:@x)}
    OBJA.new.instanceMethod("bar") {puts instance_variable_get(:@y)}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
1
objA.instanceMethod: bar
2
我不知道这是否对你有帮助,但是这个技巧在Ruby框架中被广泛使用。Ruby是编程语言的狂野西部,它实际上允许您忽略闭包。当您希望从某些代码中接受块,但不希望在定义块的上下文中执行块时,这非常有用——相反,您希望在创建的上下文中执行块

class OBJA
    @x = 10  #Instance variables attach themselves to whatever object is
    @y = 20  #self at the time they are created.  Inside a class, but outside
             #any defs, self is equal to the class, so these statements
             #create  what are known as 'class instance variables' (@@variables
             #aren't used in ruby because they don't behave 'correctly').


  def self.staticMethod(text, &block)
    puts "objA.staticMethod: #{text}"
    instance_eval &block  #See comment (1) below
  end

  def instanceMethod(text, &block)
    puts "objA.instanceMethod: #{text}"
    block.call
  end
end


class OBJB

  def initialize
    @x = 1
    @y = 2
  end

  def starterMethod
    OBJA.staticMethod("foo") {puts instance_variable_get(:@x)}
    OBJA.new.instanceMethod("bar") {puts instance_variable_get(:@y)}
  end
end

b = OBJB.new
b.starterMethod

--output:--
objA.staticMethod: foo
10  #<--CHECK THIS OUT!!
objA.instanceMethod: bar
2

实例_eval()用于将自变量的值更改为接收器。但self已经和OBJA一样了??instance_eval()成功完成的是更改其块看到的自变量的值!通过将块变量转换为instance_eval的块,实际上可以更改块上下文,即块代码看到的变量

此时,您的callback
OBJB.send
在类上被调用,但是您的
returnedCall
方法是一个实例方法。有两种方法可以解决此问题:

通过更改行
OBJB.send(…
to),对实例而不是类调用回调

OBJB.new.send(...
def self.returnedCall(text)
或者通过将
def returnedCall(…
更改为

OBJB.new.send(...
def self.returnedCall(text)

目前,您的callback
OBJB.send
在类上被调用,但您的
returnedCall
方法是一个实例方法。有两种方法可以解决此问题:

通过更改行
OBJB.send(…
to),对实例而不是类调用回调

OBJB.new.send(...
def self.returnedCall(text)
或者通过将
def returnedCall(…
更改为

OBJB.new.send(...
def self.returnedCall(text)

此时,类上调用了回调函数
OBJB.send
,但是