u16suzuの blog

日々学んだことのメモブログです。

2019-01-01から1年間の記事一覧

Alfredのメモ用のワークフローをかいた

DropboxとAlfred、Atomを組み合わせて自前のメモツールとしている。 find_fileのワークフロー メモファイルをファイル名で検索し、検索結果をAlfredに表示し、選択してAtomで開く。 script filter languageはruby, with input as {query} を指定する。 requi…

Rubyで等差数列と等比数列を書いてみる

inject便利だ。 # 等差数列 def f(a, d, c) c.times.map{|i| a + (d * i)} end p f(1, 1, 5) # 等比数列 def f2(a, r, c) c.times.map{|i| (a * r **i)} end p f2(1, 3, 5) # 等比数列 DP ver def f3(a, r, c) c.times.inject([]){|res, _| res << (res.last…

ARの自己結合と関連をcreate

自己結合 自己テーブルを参照する関連も作ることができる class AddColumnToPosts < ActiveRecord::Migration[6.0] def change add_reference :posts, :prev_post add_reference :posts, :next_post end end class Post < ApplicationRecord belongs_to :cat…

ARのrelationのメソッド定義

Association extensions # Post::ActiveRecord_Associations_CollectionProxy に hoge メソッドを生やす. # 当然メソッド内でのselfはPst::ActiveRecord_Associations_CollectionProxyで posts が入っている。 # CollectionProxyに生えているので 当然 Post.…

自分の.pryrcメモ

# encoding: utf-8 # require "~/.pry/bokudora.rb" ### エイリアス Pry.commands.alias_command 'c', 'continue' Pry.commands.alias_command 'n', 'next' Pry.commands.alias_command 's', 'step' Pry.commands.alias_command 'f', 'finish' Pry.commands.…

Railsで使っているgemの内部で実行を止める

以下をコードの止めたいところに書けば良い。 メソッドの定義内に書いても問題ない。 require "pry"; binding.pry コードを確認し終わって、元に戻したい時は以下を実行すれば良い。 $ bundle pristine GEM_NAME

cのソースをビルド&runする fish shell のコマンド

function gcc_build -d "cのソースをビルド&runする" set c_file $argv set o_file (string replace ".c" ".o" $c_file) gcc $c_file -o $o_file eval ./$o_file end

so that 構文の4種類の用法

当時、筆者が中学で習った so that 構文は「とても~なので…です」 と1種類だけなので容易に覚えることができました。 しかしながら、仕事で英語に触れる機会が増えてくると、上と似たような so that が出てくる文章ではあるけれども、上の意味で訳すとどう…

Rubyでグラフを表現してみる

class Vertex attr_accessor :id, :edges def initialize(id, edges=[]) @id = id @edges = edges end def print puts "#{ @id }" @edges.each do |edge| puts edge end end end class Edge attr_accessor :id, :from, :to, :length def initialize(id, from…

Kernel#systemと`` の挙動の違い

戻り値 p system "ls" # => true p `ls` # => "Capfile\nGemfile\nGemfile.lock\n...." エラー時の挙動 p system "ls2" # => nil p `ls2` # => No such file or directory - ls2 (Errno::ENOENT) の例外が発生し、 空文字列がかえる p ls2 の例については be…

RubyMineでファイルのシンタックスハイライトが効かないときの対応方法

bash のコードは拡張子がなくてRubyMine側がファイルの種類を判断できなかったりするので、自分で指定する必要がある。 Help > Find Action... > Action > Associate with File Type... で指定できる。 Find Action は shift 2回でも起動できる。

RubyMineのメモリ容量を増やす設定

Help > Edit Custom VM Options... をクリック メモリ容量を増やす -Xms1024m -Xmx2048m

ローカルにある不要になった remote-tracking branch を削除する

git

remote-tracking branch を削除する方法 remote-tracking branch とはローカルにあるリモートレポジトリの追跡ブランチのことです。 私たちが手元で使うローカルブランチは、 git fetch して、このremote-tracking branch をローカルに作成した後に、 ここか…

Rubyの細かい記法のメモ

begin, end で複数行をインスタンス変数にキャッシュできる def calc_camage(base) # 最終値={基本値×(1-1/8)×武器補正×攻撃回数} base * (1-1/8) * 8 * 1 end def damage @damage ||= begin atk = 12 dfn = 9 base = atk/2 - dfn/4 calc_camage(base) …

Rubyで別ファイルにログを出力する

Railsで開発をしていると、たくさんSQLを実行しているサーバだと development.log の出力が多すぎて追うのが大変になります。 こういう時、別ファイルに出力させたいのですが、以下のように行えばOKです。 また、log/ 以下は gitignore されていることがほと…

size,count,empty? のパフォーマンスを確認する

size,count,empty? のパフォーマンスを確認をしてみる。 require "benchmark" count = 1000_000 ar = [1, 2, 3, 4, 5] Benchmark.bm(10) do |x| # 10はラベルの為に確保する長さ x.report("size") do count.times do ar.empty? end end x.report("count") do…

gem のコードを git clone して RubyMine で開くスクリプト

以下を /usr/local/bin に置いて、 chmod a+x clone_and_open.rb しておく。 clone_and_open.rb https://github.com/u16suzu/foo すると ~/src にダウンロードされる。 clone_and_open.rb #!/usr/bin/env ruby class CodeReader # clone and open # # git re…

2018年12月にリリースされたRubyGems, BundlerのRubyの対応バージョンのメモ

2018年12月にリリースされた RubyGems v3系, Bundler v2系にてRubyの対応バージョンが2.3以上必須となった。 早速この影響でTravisCIが落ちるなどしたのでシュッと対応しておいた。 RubyGems v3系 (3.0.0) Ruby >= 2.3.0 Bundler v2系 (2.0.0) Ruby >= 2.3.0…