From: Robert Klemme Date: 2010-11-12T22:28:25+09:00 Subject: Re: Ruby vs PHP for the web On Fri, Nov 12, 2010 at 1:54 PM, Josh Cheek wrote: > 2010/11/12 Jesús Gabriel y Galán > >> On Thu, Nov 11, 2010 at 11:50 PM, Mike Stephens >> wrote: >> > Charles Calvert wrote in post #960599: >> >>  Ruby uses >> >> strong, though dynamic, typing. >> > >> > Can you explain what you mean? That seems a contradiction in terms. >> >> http://en.wikipedia.org/wiki/Strong_typing >> >> It means that the language imposes restrictions about how different >> types are mixed, and avoids implicit conversions of types, among other >> things. Compare: >> >> Javascript (weak typing): >> >> 2 + '2' => '22' >> >> Ruby (strong typing): >> >> 2 + '2' => TypeError: String can't be coerced into Fixnum >> > > > Is Ruby strongly typed? > > 2 + 2.0 # => 4.0 > > Here it has implicitly converted the integer 2 to a float, in order to be > able to add them. Strongly typed because there is no _implicit_ conversion. > You can even make your specific example pass, exactly as > written, by defining String#coerce: Exactly: this is an *explicit* conversion of types. Implicit conversion would be if Ruby would convert 2 to 2.0 *before* invoking a "float + operator". But in Ruby just method :+ is invoked on instance 2 which internally uses the coerce mechanism to (hopefully) get two instance which can properly work out how to +. See http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html > class String >  def coerce(num) >    return num.to_s , self >  end > end > > 2 + '2' # => "22" There seems to be some disagreement what exactly "strongly" and "weakly" means here. This becomes apparent of you read the complementary article on http://en.wikipedia.org/wiki/Weak_typing in Wikipedia. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/