Type casting and connversion in ruby

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.

“12”.to_i #=> 12

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

Ruby does not support casting.

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

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

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

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

On Sat, Mar 9, 2013 at 11:56 AM, Kumar R. [email protected] 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”

On Sat, Mar 9, 2013 at 9:08 PM, Hans M. [email protected]
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.

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/019-Complete_Numeric_Class.html

Kind regards

robert