franzejr / best-ruby Goto Github PK
View Code? Open in Web Editor NEWRuby Tricks, Idiomatic Ruby, Refactoring and Best Practices
Home Page: http://franzejr.github.io/best-ruby/
Ruby Tricks, Idiomatic Ruby, Refactoring and Best Practices
Home Page: http://franzejr.github.io/best-ruby/
As started here. We should probably have categories for a bunch of tricks. We may start using some categories, such as: https://youtu.be/aBgnlBoIkVM?t=2m29s
best-ruby.com domain is expired
So far, we just have one best practice for pure Ruby.
Looking forward to see more best practices.
The chapter on combining objects suggests that reduce and inject are different methods for different tasks.
Hi,
It will be better if we display all options list in README like faker gem.
There are 2 advantage of this
Easy to understand.
Decrease one step in search engine.
Is it make sense?
thoughts?
I can create PR, just let me know
On the page addressing Fixnum#times
you state that the developer should favor for
loops over Fixnum#times
. We generally avoid for loops in Ruby and I've often heard people argue that they should be removed entirely. I think an entire section discussing looping in Ruby would be appropriate. There you could mention that we stay away from for
.
Hi, It seems more convenient to read them in a row without opening each file. What do you think?
Probably add a description in the file.
In "Idiomatic Ruby / Combine..." the first statement is extraneous.
hash = {} # not needed, even misleading
values.inject({}) do |hash, value|
hash.merge(value => value ** 2)
end
In older versions of ruby, the hash var would collide with the block arg; in modern ruby, they're scoped, so the hash being injected is the {} arg to inject, not the variable.
A more idiomatic version might be:
result = values.inject({}) do |hash, value|
hash.merge(value => value ** 2)
end
It would also be useful to note that the use of merge with inject is a bit tricky, and only works because merge returns the hash itself. If the last expression in the block doesn't return the injected value, the inject will fail in weird ways. A less error-prone way to code it is to always return the injected value:
result = values.inject({}) do |hash, value|
hash[value] = value ** 2
hash
end
or, as a one-liner:
result = values.inject({}) {|hash, value| hash[value] = value ** 2; hash}
This is suggestion to convert this repo into gitbook (https://www.gitbook.com/)
Once converted it will be easier to browse and can be published as webpage. For example http://developer.gitbook.com/index.html
Why would you ever do
retval = if vals.any?
do_something(vals)
else
do_something_different
end
over
retval = vals.any? ? do_something(vals) : do_something_different
I think the inner assignment conditional only makes sense when more logic needs to be performed inside the conditions
try
is not ruby trick.
I don't know if this is "tricky" enough but I thought this was cool
(head, *tail) = array # break off the first element of the array, and keep the rest separately
This code:
users_ids = users.map &:id
Is anything but idiomatic. Something like this:
users_ids = users.map(&:id)
would look like proper Ruby.
Freely floating &:
just looks weird.
If we're talking about idiomatic English it should probably also be user_ids
as mentioned in #50
We should add more refactoring techniques.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.