http://programmingisterrible.com/post/42432568185/how-to-par...
http://programmingisterrible.com/post/42432568185/how-to-par...
Neither. It's a message sent to an object. The object decides how to respond to that message.
Caring about the implementation of the message receiver breaks information hiding. If you have to distinguish between .message and .message(), you already know more than you need to know to interact with that object.
Every language is confusing until you understand the mental model (even if you disagree with the utility of that model). If you come to Ruby thinking in Java/C++/C terms, you'll be unhappy, because the mental model is very different.
For Ruby, the book to read is The Well-Grounded Rubyist, which makes the language quite obvious.
Variables and methods are treated differently. I can't pass arguments to a variable, I can't even write foo(), but if it were a method I could. I can't treat variables like any other object because they have different rules applied.
I'm confused -- if you're knowingly invoking a method, why are you unsure whether it's a method or a variable?
I programmed for years in Ruby too. I just accepted a whole bunch of stuff as mysterious and shrugged. It didn't mean the language was ambiguous, I just didn't have any idea of what the actual model was.
foo();
int foo;
(int)foo;[0] your method declaration has a small problem, it apparently cannot have parenthesis around returned type.
I know syntax shouldn't matter, but personally I think consistency is very important.
I'm going to a meetup in a couple of days where Elixir and Phoenix will be presented, I think I'll keep your comment in mind and maybe ask about inconsistencies in the syntax, as I wasn't aware of this. What I know of the syntax relation to Ruby is that it is only superficial - the semantics are very different.
Let's take rust as a counter example. IMHO this is ugly:
fn foo() {...}
To make it elegant I should be able to remove the parenthesis because there's no argument inside: fn foo {...}
But this will come up with an error: "expected one of `(` or `<`, found `{` ...", which is fine. It's easy to parse, but it is not elegant IMHO.Elegance != easy parsing.
This isn't a tradeoff worth making in all languages. But it is a tradeoff, and not a rejection of elegance.
Ruby's syntax is very short and concise if you compare it to other languages. To know if "x" is a variable or a method you just need to see if "x" was assigned a value in the method, that's all.
I see.....
Anyway, this is elegant to me:
sum ||= (1..100000).to_a.inject(:+)
Maybe you have a different aesthetical sense than the rest of us. That's perfectly fine, we don't have to like the same things.
But since you questioned, now I'm curious and I would like to ask you: what language in your opinion has the most elegant syntax? Also if you have 2 extra minutes free, would you mind to code that same one-liner above in that language so we could compare the two?
(reduce + (range 1 100001))
but you can make yours a little nicer by relying on a Range being enumerable, hence:
(1..100000).inject(:+)
+/>:i.100001
You could takeout the >: but then the range is 0 indexed and not 1-based: +/i.100001The Lisp family, Scheme in particular.
(define sum (reduce + 0 (iota 100000 1)))