What is the point of object.presence?

后端 未结 5 975
太阳男子
太阳男子 2021-02-06 20:33

In the Rails docs, the example provided for the object.presence method is:

region = params[:state].presence || params[:country].presence || \'US\'
<         


        
5条回答
  •  故里飘歌
    2021-02-06 21:05

    As another example, presence lets me present my favorite FizzBuzz solution:

    puts 1.upto(100).map { |n| "#{'Fizz' if n%3==0}#{'Buzz' if n%5==0}".presence || n }
    

提交回复
热议问题