module Kernel
def Foo(*objs)
Foo.new(objs)
end
end
This is a valid solution to the problem, but I’m wondering why you
want to do type casting. Typically if you’re working in Ruby and
you’re looking at that kind of declaration, you’ve come from C# or
Java or something and you just aren’t in the groove yet. If you give
us some context, we might be able to give you a better push.
I wrote a gem called Valuable ( http://valuable.mustmodify.com/ ) that
implements very simple type casting for instance attributes. It's
useful in situations where like gets and in Rails where your input
often comes in as a string and you don't want to have to worry with
the logic of casting nil.to_i on accident, etc.
I’m not sure if there’s another way one could achieve that, but works for
the purpose.
That’s typically how they are done. And I believe methods are also made
private to disallow calling them with self:
irb(main):001:0> Integer(“1”)
=> 1
irb(main):002:0> self.Integer(“1”)
NoMethodError: private method Integer' called for main:Object from (irb):2 from /usr/local/bin/irb19:12:in’
irb(main):003:0>
And you should probably forward the block if given. So the complete
idiom looks like this:
module Kernel
def Foo(*objs, &b)
Foo.new(objs, &b)
end
private :Foo
end
Cheers
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.