Good question!
In my opinion, the more intuitive and fast the code, the better the software was built. I will show you how I express my thoughts with Ruby in small pieces of code. More here
Map
We can use the display method in different ways:
user_ids = users.map { |user| user.id }
Or:
user_ids = users.map(&:id)
Example
We can use the rand method:
[1, 2, 3][rand(3)]
In random order:
[1, 2, 3].shuffle.first
And the idiomatic, simple and easy way ... sample!
[1, 2, 3].sample
Double pipes / demounting
As you said in the description, we can use memoization:
some_variable ||= 10 puts some_variable # => 10 some_variable ||= 99 puts some_variable # => 10
Static Method / Class Method
I like to use class methods, I believe that this is really an idiomatic way to create and use classes:
GetSearchResult.call(params)
Simple Nice. Intuitively. What happens in the background?
class GetSearchResult def self.call(params) new(params).call end def initialize(params) @params = params end def call
For more information to write the idiomatic Ruby code, read here.
leandrotk Apr 22 '17 at 0:30 2017-04-22 00:30
source share