Ruby:如何制作公共静态方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13767240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:08:32  来源:igfitidea点击:

Ruby: How to make a public static method?

ruby-on-railsruby

提问by Tom

In Java I might do:

在 Java 中,我可能会这样做:

public static void doSomething();

And then I can access the method statically without making an instance:

然后我可以静态访问该方法而无需创建实例:

className.doSomething(); 

How can I do that in Ruby? this is my class and from my understanding self.makes the method static:

我怎样才能在 Ruby 中做到这一点?这是我的课程,根据我的理解self.,该方法是静态的:

class Ask

  def self.make_permalink(phrase)
    phrase.strip.downcase.gsub! /\ +/, '-'
  end

end

But when i try to call:

但是当我尝试打电话时:

Ask.make_permalink("make a slug out of this line")

I get:

我得到:

undefined method `make_permalink' for Ask:Class

Why is that if i haven't declared the method to be private?

如果我没有将方法声明为私有,为什么会这样?

回答by devanand

Your given example is working very well

您给出的示例运行良好

class Ask
  def self.make_permalink(phrase)
    phrase.strip.downcase.gsub! /\ +/, '-'
  end
end

Ask.make_permalink("make a slug out of this line")

I tried in 1.8.7 and also in 1.9.3 Do you have a typo in you original script?

我在 1.8.7 和 1.9.3 中都试过你的原始脚本有错别字吗?

All the best

祝一切顺利

回答by Ivailo Bardarov

There is one more syntax which is has the benefit that you can add more static methods

还有一种语法的好处是您可以添加更多静态方法

class TestClass

  # all methods in this block are static
  class << self
    def first_method
      # body omitted
    end

    def second_method_etc
      # body omitted
    end
  end

  # more typing because of the self. but much clear that the method is static
  def self.first_method
    # body omitted
  end

  def self.second_method_etc
    # body omitted
  end
end

回答by jefflunt

Here's my copy/paste of your code into IRB. Seems to work fine.

这是我将您的代码复制/粘贴到 IRB 中的内容。似乎工作正常。

$ irb
1.8.7 :001 > class Ask
1.8.7 :002?>   
1.8.7 :003 >   def self.make_permalink(phrase)
1.8.7 :004?>     phrase.strip.downcase.gsub! /\ +/, '-'
1.8.7 :005?>   end
1.8.7 :006?>   
1.8.7 :007 > end
 => nil 
1.8.7 :008 > Ask.make_permalink("make a slug out of this line")
 => "make-a-slug-out-of-this-line"

Seems to work. Test it out in your irbas well, and see what results you're getting. I'm using 1.8.7 in this example, but I also tried it in a Ruby 1.9.3 session and it worked identically.

似乎工作。也在你的irb身上测试一下,看看你得到了什么结果。我在这个例子中使用了 1.8.7,但我也在 Ruby 1.9.3 会话中尝试过它,它的工作原理相同。

Are you using MRI as your Ruby implementation (not that I think that should make a difference in this case)?

您是否使用 MRI 作为您的 Ruby 实现(不是我认为在这种情况下应该有所作为)?

In irbmake a call to Ask.public_methodsand make sure your method name is in the list. For example:

irb调用Ask.public_methods并确保您的方法名称在列表中。例如:

1.8.7 :008 > Ask.public_methods
 => [:make_permalink, :allocate, :new, :superclass, :freeze, :===, 
     ...etc, etc.] 

Since you also marked this as a ruby-on-railsquestion, if you want to troubleshoot the actual model in your app, you can of course use the rails console: (bundle exec rails c) and verify the publicness of the method in question.

由于您也将此标记为ruby-on-rails问题,因此如果您想对应用程序中的实际模型进行故障排除,您当然可以使用 rails 控制台:( bundle exec rails c) 并验证所讨论方法的公开性。

回答by K M Rakibul Islam

I am using ruby 1.9.3 and the program is running smoothly in my irb as well.

我正在使用 ruby​​ 1.9.3 并且该程序在我的 irb 中也运行顺利。

1.9.3-p286 :001 > class Ask
1.9.3-p286 :002?>     def self.make_permalink(phrase)
1.9.3-p286 :003?>         phrase.strip.downcase.gsub! /\ +/, '-'
1.9.3-p286 :004?>       end
1.9.3-p286 :005?>   end
 => nil 
1.9.3-p286 :006 > Ask.make_permalink("make a slug out of this line")
 => "make-a-slug-out-of-this-line"

It's also working in my test script. Nothing wrong with your given code.It's fine.

它也适用于我的测试脚本。您给定的代码没有问题。没关系。