在Ruby中,“new”和“initialize”之间的关系是什么?如何在初始化时返回nil? [英] In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

查看:128
本文介绍了在Ruby中,“new”和“initialize”之间的关系是什么?如何在初始化时返回nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是:

  obj = Foo.new(0)#=> nil或false 

这不起作用:

  class Foo 
def initialize(val)
return nil if val == 0
end
end



我知道在C / C ++ / Java / C#中,我们不能在构造函数中返回一个值。



但我想知道在Ruby是否可能。

解决方案


在Ruby中,' new '和'初始化'之间的关系是什么?


new 通常会调用 initialize new 的默认实现类似于:

  $ b def new(* args,& block)
obj = allocate

obj.initialize(* args,& block)
#实际上,这是obj.send (:initialize,...)因为初始化是私有的

obj
end
end

但你可以,当然,重写它做任何你想要的。


如何返回nil while我想要的是:

  obj = Foo。 new(0)#=> nil或false 

这不起作用:

  class Foo 
def initialize(val)
return nil if val == 0
end
end



我知道在C / C ++ / Java / C#中,我们不能在构造函数中返回一个值。



但我想知道在Ruby是否可能。


在Ruby中的构造函数。在设计良好的语言中,构造函数是不必要的。在Ruby中,只有方法,当然方法可以返回值。



你看到的问题只是你想改变返回一个方法的值,但是你正在覆盖一个不同的方法。 当然不起作用。如果你想改变方法 bar 的返回值,你应该覆盖 bar ,而不是其他一些方法。 p>

如果你想改变 Foo :: new 的行为,你应该改变 Foo :: new

  class Foo 
def self.new(val)
return nil if val.zero?
super
end
end

是一个非常糟糕的主意,因为它违反了 new 的合同,这是返回一个完全初始化的,完全正常运行的类的实例。

What I want is:

obj = Foo.new(0)  # => nil or false

This doesn't work:

class Foo
  def initialize(val)
    return nil if val == 0
  end
end

I know in C/C++/Java/C#, we cant return a value in a constructor.

But I'm wondering whether it is possible in Ruby.

解决方案

In Ruby, what's the relationship between 'new' and 'initialize'?

new typically calls initialize. The default implementation of new is something like:

class Class
  def new(*args, &block)
    obj = allocate

    obj.initialize(*args, &block)
    # actually, this is obj.send(:initialize, …) because initialize is private

    obj
  end
end

But you can, of course, override it to do anything you want.

How to return nil while initializing?

What I want is:

obj = Foo.new(0)  # => nil or false

This doesn't work:

class Foo
  def initialize(val)
    return nil if val == 0
  end
end

I know in C/C++/Java/C#, we cant return a value in a constructor.

But I'm wondering whether it is possible in Ruby.

There is no such thing as a constructor in Ruby. Constructors are unnecessary in a well-designed language. In Ruby, there are only methods and of course methods can return values.

The problem you are seeing is simply that you want to change the return value of one method but you are overriding a different method. Of course that doesn't work. If you want to change the return value of method bar, you should override bar, not some other method.

If you want to change the behavior of Foo::new, then you should change Foo::new:

class Foo
  def self.new(val)
    return nil if val.zero?
    super
  end
end

Note, however, that this is a really bad idea, since it violates the contract of new, which is to return a fully initialized, fully functioning instance of the class.

这篇关于在Ruby中,“new”和“initialize”之间的关系是什么?如何在初始化时返回nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆