Petr Skocik
Petr Skocik

Reputation: 60067

General Ruby conversions

  1. How does Integer(543543534) work given that Integer is a class? It doesn't seem to work with every class:

     Numeric '4532432'
     #=> NoMethodError: undefined method `Numeric' for main:Object
    
  2. Is there a general way to construct an instance of a class SomeClass from SomeClass and a string 'somestring' besides trying SomeClass.new('somestring') or defining #to_X?

Upvotes: 0

Views: 40

Answers (2)

Rustam Gasanov
Rustam Gasanov

Reputation: 15791

When you call Integer(543543534), you invoke Kernel#Integer method, which converts argument to Fixnum or Bignum. And Kernel module is being included by Object, so this is just a method call on Object, it isn't related to Integer class.

Upvotes: 2

sawa
sawa

Reputation: 168179

  1. Your assumption does not hold. Integer is not a class there.
  2. You have to define that as a method (in Kernel).

Upvotes: 2

Related Questions