Skip to content

Instantly share code, notes, and snippets.

View dmshvetsov's full-sized avatar
🪄

Dmitry Shvetsov dmshvetsov

🪄
View GitHub Profile

for-of, early termination, iterators, and generators

If we terminate a loop early, for-of invokes method .return() of the iterator. Generators return generator objects (that implement both the Iterable interface and the Iterator interface). If we invoke .return() on such an object, the generator is terminated. We can prevent that by overriding .return().

More information: https://exploringjs.com/es6/ch_iteration.html#sec_iteration-protocol-in-depth

Demonstration:

function logIter(iter) {
@dmshvetsov
dmshvetsov / shell_keyboard_shortcuts.txt
Created July 8, 2017 12:53
Shell keyboard shortcuts
CTRL+u clean up current command up to the start of CLI
CTRL+k up to the end of CLI
CTRL+y paste what was removed recently
CTRL+a jump to beginning of current command
@kevinadi
kevinadi / mongodb-ssl.sh
Last active March 31, 2024 15:07
Script to create self-signed CA certificates, server certificates, and client certificates for testing MongoDB with SSL
#!/bin/sh
# Generate self signed root CA cert
openssl req -nodes -x509 -newkey rsa:2048 -keyout ca.key -out ca.crt -subj "/C=AU/ST=NSW/L=Sydney/O=MongoDB/OU=root/CN=`hostname -f`/emailAddress=kevinadi@mongodb.com"
# Generate server cert to be signed
openssl req -nodes -newkey rsa:2048 -keyout server.key -out server.csr -subj "/C=AU/ST=NSW/L=Sydney/O=MongoDB/OU=server/CN=`hostname -f`/emailAddress=kevinadi@mongodb.com"
# Sign the server cert
@stympy
stympy / skcluster.rb
Created February 8, 2017 20:53
Sidekiq cluster control script and systemd service
#!/usr/bin/env ruby
require 'sidekiq'
require 'sidekiq/cli'
# Default to running one process per core
def process_count
return ENV['SK_PROCESS_COUNT'].to_i unless ENV['SK_PROCESS_COUNT'].to_i == 0
case RbConfig::CONFIG['host_os']
@charlietran
charlietran / TerminalVim.scpt
Last active October 14, 2023 06:50
Open file in iTerm vim for MacOS Sierra
-- TerminalVim.app
-- This creates a shim Application that will enable you to open files from the Finder in vim using iTerm
-- To use this script:
-- 1. Open Automator and create a new Application
-- 2. Add the "Run Applescript" action
-- 3. Paste this script into the Run Applescript section
-- 4. Save the application as TerminalVim.app in your Applications folder
-- 5. In the Finder, right click on a file and select "Open With". In that window you can set TerminalVim as a default
@rescribet
rescribet / react_ujs.js
Created July 20, 2015 23:38
Non-global react_ujs
/*globals React, Turbolinks*/
/* Modified react_ujs to prevent the components polluting global scope whenever possible.
* Since I use subdirs for my components, it also flattens the structure making
* _componentStore[className] possible.
* Creds for the react_ujs.js file to the people from react-rails (https://github.com/reactjs/react-rails)
*/
var path = require('path');
@zetavg
zetavg / preference.rb
Last active August 29, 2015 14:05
ActiveAdmin rails-settings-cached
ActiveAdmin.register_page "Preference" do
menu priority: 2
content do
form :action => admin_preference_update_path, :method => :post do |f|
f.input :name => 'authenticity_token', :type => :hidden, :value => form_authenticity_token.to_s
panel "Preference" do
fieldset do
ol do
li do
@bloudermilk
bloudermilk / memoized-helper-methods.md
Last active March 9, 2023 02:28
Explaining the rationale behind using memoized helper methods for controller resources

Last year I started playing around with using memoized private helper methods in my controllers instead of the traditional instance variable assigns we see in RoR controllers. Here's an example:

class PostsController < ApplicationController
  helper_method :new_post, :post, :posts
  
  def new; end
  def show; end
  def edit; end
 def index; end
@joyrexus
joyrexus / README.md
Last active February 19, 2024 17:15 — forked from liamcurry/gist:2597326
Vanilla JS equivalents of jQuery methods

Sans jQuery

Events

// jQuery
$(document).ready(function() {
  // code
})
@alexbevi
alexbevi / pre-commit.sh
Created August 23, 2012 12:05
Git pre-commit hook that checks ruby source files for Pry breakpoints
# Git pre-commit hook to check all staged Ruby (*.rb/haml/coffee) files
# for Pry binding references
#
# Installation
#
# ln -s /path/to/pre-commit.sh /path/to/project/.git/hooks/pre-commit
#
# Based on
#
# http://codeinthehole.com/writing/tips-for-using-a-git-pre-commit-hook/