代码之家  ›  专栏  ›  技术社区  ›  Cody Speroff

如何在rails控制台中编写函数?

  •  1
  • Cody Speroff  · 技术社区  · 9 年前

    我试图在rails控制台中编写一个函数,在本例中,这就是该函数在终端中的外观。

    >> def string_message(str = '')
    >>   return "It's an empty string!" if str.empty?
    >>   return "The string is nonempty."
    >> end
    

    >> def string_message(str = '') \n\t return "It's an empty string!" if str.empty? \n\t blah blah \n\t 
    

    ?

    1 回复  |  直到 9 年前
        1
  •  3
  •   Nina Satragno    9 年前

    IRB,ruby控制台,其中 rails console 它支持开箱即用。

    只需键入函数声明,按enter键,然后逐行输入正文,最后键入 end .

    您将看到如下文本:

    2.4.1 :001 > def say_hi(person)
    2.4.1 :002?>   puts "Hi #{person}"
    2.4.1 :003?>   end
     => :say_hi 
    2.4.1 :004 > say_hi("Nina")
    Hi Nina
     => nil 
    2.4.1 :005 > 
    

    ?