From: "Rule.rule" Date: 2008-02-21T12:19:56+09:00 Subject: Re: Ruby type-safe? Ruby strongly/weakly typed? Ruby pitfalls? On Feb 20, 5:18 pm, Moises Trovo wrote: > On Sun, Feb 17, 2008 at 7:59 PM,Rule.rule wrote: > > On Feb 6, 7:56 am, Eivind Eklund wrote: > > > On Feb 6, 2008 2:20 AM,rule.rule.r...@gmail.com > > > > wrote: > > > > Hi, > > > > > I added some paragraphs to theRubyarticle on Wikipedia: > > > > > Type system > > > > - IsRubytype-safe? > > > > - IsRubystrongly typed? > > > > Def 8 seems to be wrong; you're looking at what a variable refers to, > > > rather than the object type. The object type is constant throughout > > > object lifetime inRuby(excepting some hacks that I think are > > > C-based). > > > > Eivind. > > > OK, I see what you mean... > > So this might then be a better example of the type of an object > > changing at runtime inRuby... > > > x = 4 > > > class Fixnum > > def twice > > to_i * 2 > > end > > end > > > puts x.twice > > irb(main):001:0> 4.class > => Fixnum > irb(main):002:0> (4.to_i*2).class > => Fixnum > > The type doesn't change in your example. Well, I certainly beg to disagree. By any meaningful type-semantics, the type must be considered to change when the set of methods it responds to changes. That Ruby still calls it by the same name, apparently, only aggravates the confusion here. I guess, in Ruby, a type is best defined by its behaviour? Before the redefinition of Fixnum above, for example, calling 3.twice would throw an exception, while after, it will return a value, which is obviously a different behaviour.