Forum: Ruby type casting and connversion in ruby

Posted by Kumar R. (kumar_r)
on 2013-03-09 20:56
In programming world type casting and type conversion has completely
different meaning we all know. Now in ruby's world which it supports?
Does ruby supports both or either of them?

With the below code what it did - casting or conversion?

"12".to_i #=> 12
"abc12".to_i #=> 0
"12".to_i.class #=> Fixnum
"abc12".to_i.class #=> Fixnum
12.to_s.class #=> String

If it supports both as like C then please provide me some examples for
casting and conversion too.
Posted by Matt Mongeau (halogenandtoast)
on 2013-03-09 21:06
(Received via mailing list)
"12".to_i #=> 12

This is creating a brand new object so this is conversion.

Ruby does not support casting.
Posted by Hans Mackowiak (hanmac)
on 2013-03-09 21:08
because ruby is very strong typed, it does not have casting per se,
so without bad Hacks an object cant change its own type/class.

so to_i and others are always conversion.
but ruby has different kinds ...
to_int is implicit conversion, but only exist for classes which are 
already a number.
to_i is explicit conversion. it exist for other classes like String too


when the core defined methods wants an Integer for sample, ruby 
automatic tries to call to_int, but not to_i for non-Integer

there are other pairs too:
to_ary - to_a
to_hash - to_h
to_str - to_s
Posted by Kumar R. (kumar_r)
on 2013-03-09 21:17
Hans Mackowiak wrote in post #1100865:

Could you bit more explain the below, just to get the full taste?

> when the core defined methods wants an Integer for sample, ruby
> automatic tries to call to_int, but not to_i for non-Integer
Posted by Hans Mackowiak (hanmac)
on 2013-03-09 21:23
sample:

[4,5,6][1.2] #=>5

this works because Array#[] calls to_int if the index is no Integer, 
thats why Numeric works too

String does define to_i but no to_int, so

[4,5,6]["1.2"] #=> TypeError
Posted by Tony Arcieri (Guest)
on 2013-03-09 21:29
(Received via mailing list)
On Sat, Mar 9, 2013 at 11:56 AM, Kumar R. <lists@ruby-forum.com> wrote:

> With the below code what it did - casting or conversion?
>
> "12".to_i #=> 12
> "abc12".to_i #=> 0
>

I wouldn't use either word for this. I'd call it "coersion".

Ruby does support a form of "casting" using the "Integer" method:

irb(main):001:0> Integer("12")
=> 12
irb(main):002:0> Integer("abc12")
ArgumentError: invalid value for Integer(): "abc12"
Posted by Hans Mackowiak (hanmac)
on 2013-03-09 21:52
hm its still a conversion because the type of the objects itself does 
not change.

but Integer() method is cool too, because it has autosupport for hex 
numbers, like:

Integer("0x1a")     #=> 26
Posted by Robert Klemme (robert_k78)
on 2013-03-10 19:27
(Received via mailing list)
On Sat, Mar 9, 2013 at 9:08 PM, Hans Mackowiak <lists@ruby-forum.com> 
wrote:
> because ruby is very strong typed, it does not have casting per se,

I disagree: there is no casting - but for other reasons: Ruby does not
have casting because it is _dynamically_ typed. You cannot get into a
situation where you need to cast an expression to another type because
there is no static type information at all.

Unfortunately in C++ land the term "casting" is also often used for
"conversion" because that is done via the cast operator.  I prefer to
use "cast" only for changes which do not affect the memory
representation, i.e. changing the type of reference in Java or
reinterpreting a bit pattern in memory as a different type.
http://en.wikipedia.org/wiki/Type_conversion

> so without bad Hacks an object cant change its own type/class.

Right.

> so to_i and others are always conversion.
> but ruby has different kinds ...
> to_int is implicit conversion, but only exist for classes which are
> already a number.
> to_i is explicit conversion. it exist for other classes like String too
>
>
> when the core defined methods wants an Integer for sample, ruby
> automatic tries to call to_int, but not to_i for non-Integer

It is more complex when you consider mathematical operators, because
then conversion usually depends on two values instead of one. See
http://blog.rubybestpractices.com/posts/rklemme/01...

Kind regards

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.