How to write this more succinctly?
source link: https://www.codesd.com/item/how-to-write-this-more-succinctly.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to write this more succinctly?
Is there a construction in RoR that would allow me to write this:
@budget_project_amounts.detect {|attributes| attributes['id'] == 659 }.attributes['amount'] if !@budget_project_amounts.detect {|attributes| attributes['id'] == 659 }.nil?
more succinctly? Without the use of a variable...
Edit: This part
@budget_project_amounts.detect {|attributes| attributes['id'] == 659 }
is irrelevant. It could be anything. I sometimes have a situation like this one, where I first need to check if something is nil, before being able to continue to get a value under it (in case it's not nil and I'm looking for a way to do this without having to repeat the first part completely. I hope this makes sense.
You could try with short circuits, for instance (to mention your example):
(bp = @budget_project_amount.find { |b| b['id'] == 12 }) && bp['amount']
The result of the expression is either nil or the amount you are searching for. Less than this I cannot think of anything.
It's called short-circuit because if the first expression evaluates to false, than the second one is not evaluated, avoiding calling the method on a nil object.
Recommend
-
152
Error 520 Ray ID: 70b00007580fa7c0 • 2022-05-14 01:53:26 UTC Web server is returning an unknown error
-
5
Entity Framework Code First Succinctly We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our...
-
7
Succinctly Series Now Part of Microsoft Visual Studio Dev Essentials!We are happy to announce that a free, curated selection of Syncfusion’s Succinctly series ebooks is now included as a member benefit...
-
5
Free eBook – Uno Platform Succinctly Free eBook – Uno Platform Succinctly ...
-
6
Interview with Uno Platform Succinctly Author Ed FreitasThe following is a short interview with Succinctly series author Ed Freitas, whose book
-
4
The following is a brief interview with Succinctly author Michael Washington, whose new book, Custom Blazor Oqtane Modules Succin...
-
9
Uno Platform Succinctly We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our
-
4
The following is a brief interview with longtime Succinctly author Alessandro Del Sole, whose book Xamarin Community Toolkit Succinc...
-
3
Milestone Reached: 200+ Free Ebooks Are Now Available in the Succinctly SeriesWith 200+ technical...
-
2
The following is a short interview with Succinctly series author Michael Washington, whose book Azure Maps Using Blazor Succinctly was published recently. You can download the book from our
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK