The Jason Salas Experience

Guam's Mr. Media - making people think, making people laugh, pissing people off

Saturday, December 31, 2005

Type "safety" of objects in Ruby

One thing that's surprised me as a newbie Ruby on Rails programmer after nearly a decade of Microsoft DNA and eventually .NET development is the type assignment and "safety" (if you want to call it that) inherent within objects. Truly OOP, everything in Ruby is an object, but consider the following Ruby statement:
"JasonSalas" * 4
This outputs "JasonSalasJasonSalasJasonSalasJasonSalas". In VBScript, where everything is a variant, this would have thrown a type mismatch error, and this would likewise be a big Exception no-no in .NET. Normal string concatenation is preserved, but this impressed me at first, both positively and negatively.

2 Comments:

  • At January 1, 2006 10:56 AM, Anonymous jonah said…

    .."Truly OOP, everything in Ruby is an object, but consider the following Ruby statement:
    "JasonSalas" * 4This outputs "JasonSalasJasonSalasJasonSalasJasonSalas"

    If you consider this statement in C# or Java for that matter:
    3 + " apples"
    which outputs "3 apples" because of the overload of the "+" operator - the statement
    "JasonSalas" * 4 is not so surprising - the concept is the same except in this case it's an overload of the "*" operator.

     
  • At January 1, 2006 11:00 AM, Blogger Jason Salas said…

    Hi Jonah,

    Yeah, that did trip me out...qhen I ran across that line of code in an online tutorial, I immediately fell into defenszive mode, expecting either type-mismatch errors or odd concatenation. That it worked the way it did blew me away.

     

Post a Comment

Links to this post:

Create a Link

<< Home