24
u/pilaf Aug 21 '24
Let's not forget that Ruby understands semicolons too, so you may as well use them to hint the interpreter, and while we're at it let's put parens around those conditions and return values too so they don't feel so naked. Oh, and self. never hurts, use it at every chance to reduce ambiguity.
def isAdult()
if (self.age >= 18);
return(true);
elsif (self.age < 18);
return(false);
end;
end;I used 5 spaces for indentation too because 2 is kinda hard to read, and 4 is an unlucky number in Japanese (I don't want to be disrespectful to Matz).
2
1
u/banana_in_the_dark Aug 21 '24
The if statement could be a little more specific. Check explicitly for
self.age === 18instead. Don’t forget the triple equals becauseself.ageisn’t 18, it’s obviously justself.age
35
32
u/LordThunderDumper Aug 21 '24
Reddit.posts.current.images.reverse
Read that top to bottom and was wondering.
12
u/poop-machine Aug 21 '24
def adult?
return false if age == 0
return false if age == 1
return false if age == 2
return false if age == 3
return false if age == 4
...
end12
u/katafrakt Aug 21 '24
def adult? [21, 19, 18].include?(age) # we won't be using other adult ages during the hackathon demo end
14
u/akira410 Aug 21 '24
Let's take it one more level:
def isAdult?(age)
Proc.new {
->(a) {
is_adult_proc = ->(val) {
if val >= 18
:adult
else
:not_adult
end
}
is_adult_proc.call(a)
}.call(age)
}.call == :adult
endUgh... I feel so dirty.
11
u/anykeyh Aug 21 '24
If the goal is to make cursed code, I love this one:
def adult? Math.sqrt(age - 18) rescue false endI love it, because I saw too often real codes following this pattern ;-) Oh, it doesn't return
true, it returnstruthyvalue, for the sake of keeping it concise.2
u/akira410 Aug 21 '24
Oh! I hate it and love it at the same time! That's a good one! Thank you for sharing it.
6
9
u/matheusrich Aug 21 '24
def isAdult()
case @age
when ...18 then
return false
when 18.. then
return true
end
end3
u/mrfredngo Aug 21 '24
Don't quite understand... Shouldn't the direction be reversed? What am I missing?
1
u/jrhorn424 Aug 22 '24
I took the top image as "idiomatic ruby" and the last image as "barely resembles (a) ruby".
1
u/Periiz Aug 21 '24
No, but the last one is actually very smart. You see even without an else clause, this is always going to work because there is no number not greater than or equal to 18 nor less than 18!
/s
1
u/MeroRex Aug 21 '24 edited Aug 21 '24
def adult?(age)
return false unless (age / 18.0 ) < 1.0 ? false : true
return true
end
This is the only way.
-5
u/Kina_Kai Aug 21 '24
I don't know if the implicit return is always best. It is nice and there are times where it fits well, but I've grown to feel that it's better to be explicit in the general case.
14
u/ignurant Aug 21 '24
There’s a certain set of design decisions that this is part of that I’ve always admired. At first, the “implicit return” and “optional parenthesis” come off as a flourish akin to choosing to be different in some “look what I can do” kind of way. But as you follow the rabbit hole, you learn that these are sort of consequences of a fundamental design decision rather than a “look how punk rock we can be”.
I started to understand some of this while they were debating a potential
|>function pipeline operator feature a few years ago. Matz made a comment regarding why we can’t capture a reference to a method as easily as in other languages. It was the first time I realized that while Ruby is quite similar to py and js, it is designed in a fundamentally different way that has real repercussions. And I started to appreciate how things like implicit return and no parens are side effects of that design, rather than just gimmicks.I’ve come to think that this design constraint and these side effects are a large contributor to Ruby’s conversational nature.
In the end, for me, Ruby is about the “conversation between objects” rather than the “instructions to do something”. This shows in the meme too. The versions descend into progressively more focus on “instructions”. When it’s about the conversation,
returndoesn’t even mean anything at all. Because of course sending a message to an object has a reply. Everything is an expression.Anyway, none of this is intended to reflect on you in particular, but your comment inspired me to get chatty.
1
u/Kahlil_Cabron Aug 21 '24
If you come from a functional programming background, implicit returns make a lot more sense. Some of the earliest programming languages only had implicit returns. It makes way more sense to me (though I like having explicit returns for early returning).
7
u/twinklehood Aug 21 '24
I hope we can at least agree that single line methods never need it?
2
u/tomc-01 Aug 21 '24
I suspect that agreement will only be after another 5 paragraphs of text discussing a single line method in ruby. 🤣
1
3
2
1
-2
u/BlueEyesWhiteSliver Aug 21 '24
I hate the last one. In more convoluted systems, it can become non-obvious of the refactor that’s possible.
0
Aug 21 '24
[deleted]
9
u/Kinny93 Aug 21 '24
…but the first option is also the easiest to understand?
0
Aug 21 '24
[deleted]
5
u/expatjake Aug 21 '24
I respectfully disagree. I’d say the first, most concise one, has least unnecessary fluff adding to the cognitive load. I expect that your criticism could be levelled at a bunch of other ruby code but I don’t think it applies here.
31
105
u/anykeyh Aug 21 '24
And now (3.2+), embrace the laziness!