Ruby: How to make a public static method?

前端 未结 4 1875
醉梦人生
醉梦人生 2021-02-01 12:15

In Java I might do:

public static void doSomething();

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

c         


        
4条回答
  •  囚心锁ツ
    2021-02-01 12:32

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

    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.

提交回复
热议问题