Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Well, I can answer at least a few of those 'WHY?!'s.

Firstly starting with my_string['foo'] = 'bar', this is a syntax that is used a lot in Ruby for anytime that you wish to recover a subset of a collection - you can use it with a Dir object for example to recover a subset of directories/files, or in the case of a hash, to recover the object stored at the key. The thing is that you need to think about it as being a subset operator, rather than an index operator.

The exclamation point at the end of a method name has no syntactic meaning - it is simply a naming convention used by the standard libraries to indicate when the method mutates the object or not. Same same for method names ending with a question mark, it's just a convention to indicate that the response will be a boolean.

As for the whole "5" * 2 vs. 2 * "5", I must admit to not understanding where you're coming from. I don't see how you could expect any other kind of behaviour from an object orientated language. Personally I don't like over-riding arithmetic operators for non-number types, for precisely this reason, but hey, some people like it, so why not?

Integers converting to strings? I think every language has that no? I mean there's always something like: sprintf(myStringPtr, "%d", myInteger); or cout << myInteger;

etc etc

By making this a standard method on all classes (to_s), Ruby makes it really easy to dump a class to a serial format, which is very useful for debugging, as an example.

I get the impression that you simply didn't spend enough time with Ruby to come to understand its idioms, in which case, you're not really in a position to effectively criticise the language...



I suppose these just seem odd to me, which was my point. Not that I find something absolutely wrong about them, anymore than I can find something wrong about a language which doesn't have mutable variables or whatever the case may be.

A lot of Ruby's conventions simply scream "ambiguity" to me. A "subset of a collection" is extremely ambiguous; if there is more than 1 foo in my_string, does it always get the first? How do I get the rest? my_string['foo'][2] or something? I can't even intuitively make something up which makes sense to me. Perhaps there isn't a way to get the others, which I suppose would be in line with Python, which doesn't supply a string method to find all occurrences at once... but how do I offset it?

I can see where standardized methods like to_a can be more convenient, but they also open up the possibility for unlimited ambiguity. What does Foo.to_a do for your Foo class? It could do anything! There's no way I could rely on these methods to do something across the board unless I know your class conforms to some Interface (do Ruby folks use/have Interfaces?). In Python, if I do

  [foo]
Then foo is now a list. I always know what I'm getting. I always know what int() is going to attempt to do to an object, etc.


Perhaps there isn't a way to get the others, which I suppose would be in line with Python, which doesn't supply a string method to find all occurrences at once... but how do I offset it?

Yes, there's no way to get the other matches using [] as far as I know, it's just a generalization of being able to pass various types to it; Fixnum, Range, Regexp.. I don't see the harm in doing something with String too. If you wanted to extract multiple items, you'd use something like String#scan, or if you wanted to do a global replace, String#gsub, which can also accept a code block to perform computed replacements.

I can see where standardized methods like to_a can be more convenient, but they also open up the possibility for unlimited ambiguity. What does Foo.to_a do for your Foo class?

As of 1.9, it does this:

    NoMethodError: undefined method `to_a' for Foo:Class
In 1.8 it issues "warning: default `to_a' will be obsolete".

In Python, if I do `[foo]` Then foo is now a list.

You do the exact same thing in Ruby, though if you're wanting to cast your argument like the old to_a did, you use Array(), which won't turn [] into [[]]. If you want a number, Integer() or Float() will raise an exception if they can't give you one.


"As for the whole "5" * 2 vs. 2 * "5", I must admit to not understanding where you're coming from. I don't see how you could expect any other kind of behaviour from an object orientated language."

Python has a __rmult__ method. If you do A * B, first it internally calls A.__mult__(B) then if that raises TypeError, it tries B.__rmult__(A). It's pretty elegant, I think. For Ruby, it wouldn't look as nice to do A.r* though, I guess, but it would be a nice bit of functionality.


>I get the impression that you simply didn't spend enough time with Ruby to come to understand its idioms, in which case, you're not really in a position to effectively criticise the language...

Well yeah like others have been saying, python is more attractive to new users is probably partly why it's more popular. You don't need to work as hard to understand its idioms, it's more readable by newbies (by anyone), etc.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: