aspose file tools*
The moose likes Ruby and the fly likes Try Ruby! 15 minutes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Ruby
Bookmark "Try Ruby! 15 minutes" Watch "Try Ruby! 15 minutes" New topic
Author

Try Ruby! 15 minutes

Sree Va
Ranch Hand

Joined: Jan 28, 2007
Posts: 38
http://tryruby.hobix.com/


We believe that every being is divine, is God. Every soul is a sun covered over with clouds of ignorance; the difference between soul and soul is owing to the difference in density of these layers of clouds. - Swami Vivekananda
Sree Va
Ranch Hand

Joined: Jan 28, 2007
Posts: 38
DON'T TYPE. YOU'LL BE STUCK FOREVER.

>> 2**2**2**2**2
Sree Va
Ranch Hand

Joined: Jan 28, 2007
Posts: 38
>> "Jimmy".to_i
=> 0

"Jimmy" when converted to int results 0. WHAT?!
Sree Va
Ranch Hand

Joined: Jan 28, 2007
Posts: 38
Whatz with 'thing'?
Doesn't it have a name, int, char, String, ............?
Hello OO- Abstraction?! Hello!
Justin Gehtland
author
Ranch Hand

Joined: Jan 30, 2007
Posts: 31
"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

Joined: Jan 28, 2007
Posts: 38
Are you saying, it doesn't matter to differentiate between "0".to_i and "Zero".to_i?
Mm.... interesting language feature.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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).


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jan 28, 2007
Posts: 38
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.")
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Try Ruby! 15 minutes