6 Matching Annotations
- Dec 2023
-
-
Compared with simple clients, modern clients are generally much easier to use and more Ruby-like
-
- Sep 2023
-
stackoverflow.com stackoverflow.com
-
You just blew my mind Marc-André. My code just went from idiotic to idiomatic.
-
- Jun 2021
-
stackoverflow.com stackoverflow.com
-
But what's the matter with "raw" instance variables? They are internal to your instance; the only code that will call them by name is code inside pancake.rb which is all yours. The fact that they start with @, which I assume made you say "blech", is what makes them private. Think of @ as shorthand for private if you like.
I agree / like that:
@
is just shorthand forprivate
.But OP clarified in a comment that the
@
itself is not what they disliked: it was the accessing data directly instead of going through an accessor method.The raw variable is the implementation, the accessor is the interface. Should I ignore the interface because I'm internal to the instance?
-
Setting an instance variable by going through a setter is good practice, and using two access modifiers is the way to accomplish that for a read-only instance variable
-
-
stackoverflow.com stackoverflow.com
-
I don't think it is too clever. I think it solves the problem idiomatically. I.e., it uses reduce, which is exactly correct. Programmers should be encouraged to understand what is correct, why it is correct, and then propagate. For a trivial operation like average, true, one doesn't need to be "clever". But by understanding what "reduce" is for a trivial case, one can then start applying it to much more complex problems. upvote.
-
Thanks, this was just what I was looking for! This is a perfect appropriate use of instance_eval. I do not understand the nay-sayers. If you already have your array in a variable, then sure, a.reduce(:+) / a.size.to_f is pretty reasonable. But if you want to "in line" find the mean of an array literal or an array that is returned from a function/expression — without duplicating the entire expression ([0,4,8].reduce(:+) / [0,4,8].length.to_f, for example, is abhorrent) or being required to assign to a local, then instance_eval option is a beautiful, elegant, idiomatic solution!!
-