In Ruby, where is `Float (...)` documented?

In Ruby, you can call Floatas a function for a reliable way to make sure the argument is a Float or parsing as a float (including scientific notation, etc.).

For instance:

Float(1.0)     # => 1.0
Float('1.0')   # => 1.0
Float('-1.23') # => -1.23
Float('-1e+2') # => -100.0

However, the Ruby docs do not seem to describe this behavior anywhere ( Float v2.1.0 , Float v1.9.3 ).

Where can I find documentation for this feature?

+3
source share
2 answers

This defines the # Float Core . This feature is part of the built-in conversion functions (a term coined by Avdi Grimm ) offered by the ruby.

" , , . , , , : - Float, nil ." , .

, Kernel # Array, , 0, 1 .

  def process_post(post_or_posts)
    posts = Array(post_or_posts)
    posts.each do |post|
      .... # do something post
    end
  end

:

 process_post("post1")
 process_post(["post1", "post2"])
 process_post(nil)

, - :

 def process_post(post_or_post)
   if post_or_post    # now we have to check for nil
      # we might have to check for instance of Array to make sure we can iterate now.
      # etc..
   end
 end

, , API.

+6

, , , . , ​​, Method#owner #inspect, , . .

method(:Float).owner # => Kernel 
method(:Float).inspect # => "#<Method: Object(Kernel)#Float>" 

ri ri Kernel.Float :

= Kernel.Float

(from ruby site)
------------------------------------------------------------------------------
  Float(arg)    -> float

------------------------------------------------------------------------------

Returns arg converted to a float. Numeric types are converted directly,
the rest are converted using arg.to_f. As of Ruby 1.8, converting nil
generates a TypeError.

  Float(1)           #=> 1.0
  Float("123.456")   #=> 123.456
+1

All Articles