## Reference: https://womanonrails.com/each-with-object
# Bad
array = []
(1..10).each do |item|
array << item ** 2
end
array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createStore, applyMiddleware, compose } from "redux"; | |
import rootReducer from "../reducers/index"; | |
import { forbiddenWordsMiddleware } from "../middleware"; | |
const storeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; | |
const store = createStore( | |
rootReducer, | |
storeEnhancers(applyMiddleware(forbiddenWordsMiddleware)) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.eager_load! | |
unused_routes = {} | |
# Iterating over all non-empty routes from RouteSet | |
Rails.application.routes.routes.map(&:requirements).reject(&:empty?).each do |route| | |
name = route[:controller].camelcase | |
next if name.start_with?("Rails") | |
controller = "#{name}Controller" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Bad | |
range = 1..Float::INFINITY | |
p range.collect { |x| x * x }.first(10) | |
# Good | |
range = 1..Float::INFINITY | |
p range.lazy.collect { |x| x * x }.first(10) | |
Reference: http://patshaughnessy.net/2013/4/3/ruby-2-0-works-hard-so-you-can-be-lazy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Rails, ActiveRecord, query id in array of ints, keep order of passed array | |
# way 1 | |
User.where(id: ids).sort_by { |u| ids.index(u.id) } | |
# way 2 | |
User.where(id: ids).order("position(id::text in '#{ids.join(',')}')") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add data attribute to option | |
<%= select_tag :addon, options_for_select(@addons.map {|d| [d.name, d.sku, {'data-addon' => d.sku.downcase }]}) %> |