Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Try Ruby! 15 minutes

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://tryruby.hobix.com/
 
Sree Va
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DON'T TYPE. YOU'LL BE STUCK FOREVER.

>> 2**2**2**2**2
 
Sree Va
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> "Jimmy".to_i
=> 0

"Jimmy" when converted to int results 0. WHAT?!
 
Sree Va
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatz with 'thing'?
Doesn't it have a name, int, char, String, ............?
Hello OO- Abstraction?! Hello!
 
author
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jimmy".to_i == 0

This might be one of those "paradigm shifts" asked about earlier. ;-)

There's two ways you can go with an attempt to convert a non-numeric string into an integer: you can throw an exception, or you can return an innocous, documented, well-known answer. Java very often goes with the former approach; trying to parse a string into an integer when it can't results in an exception. Ruby very often takes the latter approach; you asked for an integer, and you passed me something with no number in it, so we'll just give you 0.

Likewise, functions that are supposed to return collections of things rarely throw exceptions because of simple type conflicts; they just return empty arrays. In both cases, my code can be quite succinct because I always get back what I expect.

However, it is perfectly reasonable to prefer the former approach. It just isn't crazy to like the latter, either. ;-)
 
Sree Va
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you saying, it doesn't matter to differentiate between "0".to_i and "Zero".to_i?
Mm.... interesting language feature.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sree Va:
Whatz with 'thing'?
Doesn't it have a name, int, char, String, ............?
Hello OO- Abstraction?! Hello!


I'm not sure what you mean by "thing" but I suspect you're going through _why's tryruby tutorial and wondering why variables don't have types like "int", "char" or "String"?

In Ruby, a variable can change its type. In other words, you can do stuff like this:


This has nothing to do with the language being object-oriented or not (nor does it have anything to do with abstraction).
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sree Va:
Are you saying, it doesn't matter to differentiate between "0".to_i and "Zero".to_i?
Mm.... interesting language feature.


Yep, and it means that when we need to validate input we need to do it explicitly (which isn't a bad thing), and when we don't need to we also don't need to clutter our code with stuff like "try { ... } catch (SomeCheckedException weKnowThisCannotHappen) { }".
 
Sree Va
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:

I'm not sure what you mean by "thing" but I suspect you're going through _why's tryruby tutorial and wondering why variables don't have types like "int", "char" or "String"?

In Ruby, a variable can change its type. In other words, you can do stuff like this:


This has nothing to do with the language being object-oriented or not (nor does it have anything to do with abstraction).




How is Data Abstraction-> Abstract Data Types, User Defined Data Types got nothing to do with OO language?

Its good that Ruby does this magic of a variable taking value of any type. But we have seen it before and faced/facing challenges of such feature. And why again?!
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruby has data types, both built-in and user-defined. That's not the issue here. It's just that a single variable (as in "pointer to an object") can point at objects of different types during its lifetime.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by "challenges" (with dynamic typing) you're referring to missing out on the compile-time type checking of statically typed languages, that challenge can be eliminated with thorough automated tests.

Tongue in cheek, one might turn around and say that static typing is actually bad for you because creates a false illusion of program correctness... ("It compiles, therefore it is correct.")
 
"To do good, you actually have to do something." -- Yvon Chouinard
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic