How to extract the sign of an integer in Ruby?

前端 未结 7 2122
渐次进展
渐次进展 2020-12-30 00:49

I need a function which returns/prints the sign on an integer. So far I came up with this:

def extract_sign(integer)
  integer >= 0 ? \'+\' : \'-\'
end
         


        
7条回答
  •  悲哀的现实
    2020-12-30 01:13

    Ruby doesn't have a built in sign function like Javascript. Here's a thread that explains more http://www.ruby-forum.com/topic/141216

    Your approach looks correct.

提交回复
热议问题