Ниже будут приведены аргументы в пользу выбора Mocha
-
Высокая популярность:
— 4m против 400k загузок в месяц -
Высокая активность:
— 1 890 против 1400 коммитов (всего)
— 171 против 101 коммитов (последний год)
// ES7, async/await | |
function sleep(ms = 0) { | |
return new Promise(r => setTimeout(r, ms)); | |
} | |
(async () => { | |
console.log('a'); | |
await sleep(1000); | |
console.log('b'); | |
})() |
# Postgresql fancy datatypes! | |
* array | |
* hstore (=~ hash) | |
* json | |
* jsonb | |
Philippe Creux - [@pcreux](http://twitter.com/pcreux) |
# Originally written by Justin French (2008): | |
# http://justinfrench.com/notebook/a-custom-rake-task-to-reset-and-seed-your-database | |
# | |
# Modified to work with Rails 4. | |
desc 'Raise an error unless development environment' | |
task :safety_check do | |
raise "You can only use this in dev!" unless Rails.env.development? | |
end |
It's pretty easy to do polymorphic associations in Rails: A Picture can belong to either a BlogPost or an Article. But what if you need the relationship the other way around? A Picture, a Text and a Video can belong to an Article, and that article can find all media by calling @article.media
This example shows how to create an ArticleElement join model that handles the polymorphic relationship. To add fields that are common to all polymorphic models, add fields to the join model.
Sometimes you need to iterate over a ton of items and you don't want the overhead of creating AR objects out of all of them. Hell, you only need a few things! Well, #pluck has your back.
But what if you want to iterate over many tonnes of items?
Pluck in batches to the rescue!
This isn't the exact code that I use in my code base, but it is damn close.
// array utils | |
// ================================================================================================= | |
const combine = (...arrays) => [].concat(...arrays); | |
const compact = arr => arr.filter(Boolean); | |
const contains = (() => Array.prototype.includes | |
? (arr, value) => arr.includes(value) | |
: (arr, value) => arr.some(el => el === value) |
#!/usr/bin/env ruby | |
require 'paleta' | |
to_paleta = ->(color) { Paleta::Color.new(:hex, color) rescue nil } | |
to_div = ->(str) { | |
"<div style='font-family: sans-serif; width: 5em; height: 3em; | |
line-height: 3em; display: inline-block; text-align: center; | |
margin: 0.25em; border-radius: 0.25em; padding: 1em; | |
background: ##{str}'> |
#!/usr/bin/env ruby | |
# | |
# Ruby script to download a number of files | |
# from individual URLs via HTTP/HTTPS/FTP | |
# specified in an external file. | |
# | |
# Author: Tobias Preuss | |
# Revision: 2013-04-18 16:26 +0100 UTC | |
# License: Creative Commons Attribution-ShareAlike 3.0 Unported |
# A small DSL for helping parsing documents using Nokogiri::XML::Reader. The | |
# XML Reader is a good way to move a cursor through a (large) XML document fast, | |
# but is not as cumbersome as writing a full SAX document handler. Read about | |
# it here: http://nokogiri.org/Nokogiri/XML/Reader.html | |
# | |
# Just pass the reader in this parser and specificy the nodes that you are interested | |
# in in a block. You can just parse every node or only look inside certain nodes. | |
# | |
# A small example: | |
# |