Skip to content

Instantly share code, notes, and snippets.

class Numeric
def to_be_between(min, max)
self < min ? min : self > max ? max : self
end
end
p 4.to_be_between(5, 10) # => 5
p 5.to_be_between(5, 10) # => 5
p 8.to_be_between(5, 10) # => 8
p 10.to_be_between(5, 10) # => 10
p 11.to_be_between(5, 10) # => 10
@mugyu
mugyu / async_and_await.js
Created April 27, 2019 02:41
sample async/await
// these are samples for async/await
async function rejecting() {
throw "reject!!!!";
}
async function resolving(msg) {
await sleep(Math.random() * 10000);
return message = "resolve!" + (msg ? " -- " + msg : "");
}
resolving()
@mugyu
mugyu / sleep.js
Last active April 27, 2019 02:36
Simply JavaScript Sleep function
// JavaScript simple sleep function
// usage: await sleep(milliseconds)
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
@mugyu
mugyu / generator.js
Last active April 27, 2019 02:32
Generator in JavaScript
// JavaScript Generator
function* getGenerator(index) {
while(index < 3)
{
yield index++;
}
}
const generator = getGenerator(0);
console.log(generator);
@mugyu
mugyu / null_coalescing_operator_and_elvis_operator_in_php.php
Created April 10, 2019 06:55
PHPに於けるNull合体演算子とエルビス演算子
<?php
// ?? - null合体演算子(左辺がnull or 未定義の場合に右辺を返す)
// ?: - エルビス演算子(左辺がfalseとequalityな場合に右辺を返す)
unset($hoge);
$hoge = ($hoge ?? 1) ?: 2;
var_dump($hoge);
$hoge = NULL;
$hoge = ($hoge ?? 1) ?: 2;
@mugyu
mugyu / php.class_method_and_instance_method.php
Last active March 11, 2019 09:47
phpのクラスメソッドとインスタンスメソッド
<?php
class A {
public static function classMethod() {
echo "class method\n";
}
public function instanceMethod() {
echo "instance method\n";
}
@mugyu
mugyu / php_warning_should_be_compatible.php
Created March 11, 2019 09:42
php7: 継承したメソットの引数違いでワーニング
<?php
class A
{
protected function data(int $id)
{
return "A";
}
}
class B extends A
@mugyu
mugyu / php.what_diffrent_self_and_static.php
Created March 11, 2019 09:36
比較 self::hoge() and static::hoge()
<?php
/**
* 比較 self::hoge() and static::hoge()
*/
class A1 {
public static function message() {
return "A1\n";
}
@mugyu
mugyu / bundle_install_path.md
Created February 12, 2019 02:01
bundler の path 指定

bundler の path 指定 いつも最初に実行する度に忘れてググってしまう

$ bundle install --path vendor/bundler
@mugyu
mugyu / use_promise_all.md
Created February 11, 2019 06:22
プロミスで最低待ち時間を設定する

プロミスで最低待ち時間を設定する

下記のようにすると、 fetch()関数に掛かる時間に係わらず 最低限、1000ミリ秒は必ず待つ。

演出上の都合などで待ち時間をある程度揃えたい場合に使える。

// Promise.all で並列で処理するプロミスを全て待つので