Skip to content

Instantly share code, notes, and snippets.

$ irb
irb(main):001:0> class Fixnum
irb(main):002:1> def plus_with_two_plus_two_is_five(other)
irb(main):003:2> other += 1 if self == 2 && other == 2
irb(main):004:2> plus_without_two_plus_two_is_five(other)
irb(main):005:2> end
irb(main):006:1> alias_method :plus_without_two_plus_two_is_five, :+
irb(main):007:1> alias_method :+, :plus_with_two_plus_two_is_five
irb(main):008:1> end
Rubinius::InvalidBytecode: instruction argument is not a Fixnum: code: call, ip: 13
@ippeiukai
ippeiukai / singleton_with_class_delegator.rb
Created June 17, 2016 07:53
Singleton but as convenient as class methods; it's both.
module SingletonWithClassDelegator
extend ActiveSupport::Concern
included do
include Singleton
original_delegate_method = method(:delegate).unbind if self.respond_to?(:delegate)
extend SingleForwardable
if original_delegate_method.present?
# restore the delegate of ActiveSupport that has been overshadowed by SingleForwardable
define_singleton_method :delegate, original_delegate_method
"use strict";
const Sequelize = require('sequelize');
const Promise = Sequelize.Promise;
const DEFAULT_BATCH_SIZE = 3000;
/**
* Port of ActiveRecord::Base.find_each of Rails.
@ippeiukai
ippeiukai / check-whether-eb-master-instance.js
Created October 5, 2016 02:05
For running something only on one instance in an ElasticBeanstalk environment. Inspired by http://blog.rotaready.com/scheduled-tasks-elastic-beanstalk-cron/
"use strict";
const AWS = require('aws-sdk');
const bluebird = require('bluebird');
/**
* @returns {Promise.<boolean>} - is on 'master' instance or not
*/
module.exports = bluebird.coroutine(function* (opts) {
if (opts == null) {
@ippeiukai
ippeiukai / cron.conf
Last active July 31, 2021 21:00
Running cron in AWS ElasticBeanstalk web tier.
files:
"/tmp/crontab":
mode: "000777"
owner: 'ec2-user'
group: 'ec2-user'
content: |
30 02 * * * sudo /usr/sbin/execute-in-eb-node-app 'node bin/is-eb-master.js' && sudo /usr/sbin/execute-in-eb-node-app 'npm run daily-maintenance'
encoding: plain
container_commands:
01-copy_eb_bin:
@ippeiukai
ippeiukai / subset-of.ts
Created November 14, 2017 02:21
`SubsetOf` meta type
export type SubsetOf<T> = {
[P in keyof T]?: T[P];
};
@ippeiukai
ippeiukai / functions.php
Last active July 23, 2023 09:32 — forked from tilap/functions.php
Wordpress: How to automatically attache youtube/vimeo thumbnail as a post feature image from a post meta key
<?php
// https://github.com/WebDevStudios/Automatic-Featured-Images-from-Videos/blob/1.2.0/automatic-featured-images-from-videos.php
/**
* If a YouTube or Vimeo video is added in the post content, grab its thumbnail and set it as the featured image.
*
* @since 1.0.0
*
* @param int $post_id ID of the post being saved.
* @param string $video_thumbnail_url URL of the image thumbnail.