# Defining your own typecaster

**URL:** <https://www.ruby-forum.com/t/defining-your-own-typecaster/187531>\
**Category:** Ruby\
**Created:** [April 25, 2010, 1:38am UTC](https://www.ruby-forum.com/t/defining-your-own-typecaster/187531 "2010-04-25T01:38:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [April 25, 2010, 1:38am UTC](https://www.ruby-forum.com/t/defining-your-own-typecaster/187531/1 "2010-04-25T01:38:04Z")

</div>

Hi, how do you define your own typecaster?

Builtin examples

Array(1…5) # =\> [1, 2, 3, 4, 5]  
Integer(5.2) # =\> 5  
Float(5) # =\> 5.0

I would expect it to be something like this

class Foo  
def self.()(\*objs)  
Foo.new  
end  
end

Foo(1)

But that isn’t valid syntax, and I’m not sure what to look up to figure  
this  
out.

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [April 25, 2010, 2:02am UTC](https://www.ruby-forum.com/t/defining-your-own-typecaster/187531/2 "2010-04-25T02:02:04Z")

</div>

I’ve seen the following declaration, which I think it is valid:

module Kernel  
def Foo(\*objs)  
Foo.new(objs)  
end  
end

I’m not sure if there’s another way one could achieve that, but works  
for  
the purpose.

Vinicius Baggio F.  
[http://www.vinibaggio.com](http://www.vinibaggio.com)

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [April 25, 2010, 2:02am UTC](https://www.ruby-forum.com/t/defining-your-own-typecaster/187531/3 "2010-04-25T02:02:40Z")

</div>

On [Sun, 25.04.2010 08:37], Josh C. wrote:

> out.  
> They’re actually just methods with an uppercase name.

class Foo  
â€¦  
end

def Foo(arg)  
Foo.new(â€¦)  
end

Foo(â€¦) # =\> #Foo:0x8ebc4b4

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [April 26, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/defining-your-own-typecaster/187531/4 "2010-04-26T16:50:23Z")

</div>

> 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.

class BaseballPlayer  
has\_value :hits, :klass =\> :integer  
has\_value :runs, :klass =\> :integer  
has\_value :team  
has\_value :phone\_number, :klass =\> PhoneNumber

def rbi  
(hits / runs.to\_f) if hits && runs  
end  
end

> > joe = BaseballPlayer.new(:hits =\> ‘24’, :runs =\> ‘12’, :team =\> ‘Dragons’)

> > joe.phone\_number =\> 1234567890

> > joe.runs  
> > =\> 12

> > joe.rbi  
> > =\> 0.5

> > joe.runs = nil

> > joe.rbi  
> > =\> nil

> > joe.phone\_number  
> > =\> “(123) 456-7890”

* * *

Johnathon W.  
Web Application Consultant

[http://blog.mustmodify.com/](http://blog.mustmodify.com/)

---

<div class="post-metadata">

**Author:** ![jcain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3d9bf3/32.png) [@jcain](https://www.ruby-forum.com/u/jcain)\
**Post date:** [April 25, 2010, 12:42pm UTC](https://www.ruby-forum.com/t/defining-your-own-typecaster/187531/5 "2010-04-25T12:42:55Z")

</div>

On 04/25/2010 02:01 AM, Vinícius Baggio F. wrote:

> 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
