The file '/usr/local/bin/bundle' is marked as an executable but could not be run by the operating system.
最近は仕事では Docker 上で開発することがほとんどでローカルでRubyをいじらない。 いつからか ローカルでの bundle install が失敗するようになっていた。 bashのshebangを実行ファイルに書くと良いという情報があったが、それでもなおらなかった。
$ bundle install Failed to execute process '/usr/local/bin/bundle'. Reason: exec: Exec format error The file '/usr/local/bin/bundle' is marked as an executable but could not be run by the operating system.
とりあえずbundlerを入れ直そうと思い、削除したところなおった。
$ rm /usr/local/bin/bundler /usr/local/bin/bundle
削除後、以下のパスの bundler が使われるようになった。
$ /Users/suzuki_y/.rbenv/shims/bundle
/usr/local/bin/bundler のソースコードには、コメントで「この bundler は gem インストール時に作成したもの」と書かれていたので、 rubygems側で作ったbundlerを誤って使うようになっていたのかもしれない。
Alfredのメモ用のワークフローをかいた
DropboxとAlfred、Atomを組み合わせて自前のメモツールとしている。
find_fileのワークフロー
メモファイルをファイル名で検索し、検索結果をAlfredに表示し、選択してAtomで開く。
- script filter languageはruby, with input as {query} を指定する。
require 'json' files = `find -E ~/Dropbox/memoWork/ -type f -iregex \'.*{query}.*\'` items = [] files.split("\n").each do |file| items << { title: File.basename(file), subtitle: file, arg: file, icon: 'some_icon.png', } end res = { "items" => items }.to_json print res
指定のハッシュ形式でprintすると、Alfredの中で一覧表示し選択することができる。選択した項目はハッシュの arg の部分が run script の {query} に入る。
- run script
/usr/local/bin/atom {query}
memoのワークフロー
keyword
run script
/usr/local/bin/atom ~/Dropbox/memoWork ruby ./main.rb daily
grep_fileのワークフロー
brew install pt しておく必要がある。
run script は find_file と同じ。
require 'json' s =`/usr/local/bin/pt {query} ~/Dropbox/memoWork/ | grep -v 00_daily | grep -v 99_アーカイブ` items = [] s.split("\n").each do |line| l = line.split(":") fname = l.first arg = l.take(2).join(":") items << { title: File.basename(fname), subtitle: arg, arg: arg, icon: 'some_icon.png', } end res = { "items" => items }.to_json print res
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 || a) * r} end p f3(1, 3, 5) # 等差数列 DP ver def f4(a, d, c) c.times.inject([]) do |res, _| if res.last res << res.last + d else res << a end end end p f4(1, 1, 5) # 等差数列 Enumerator::ArithmeticSequence ver # #stepがEnumeratorを継承したEnumerator::ArithmeticSequence を返す。 # Enumerator::ArithmeticSequenceは等差数列を表現するクラス p 1.step(by: 3).take(5)
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 :category has_many :prev_posts, class_name: "Post", foreign_key: "prev_post_id" belongs_to :next_post, class_name: "Post" end
以下のメソッドが使えるようになる.
> Post.last.next_post > Post.last.prev_posts
関連をcreate
# これだけでcommitされる Category.last.posts.create( title: "a", body: "bb")
ARのrelationのメソッド定義
Association extensions
# Post::ActiveRecord_Associations_CollectionProxy に hoge メソッドを生やす. # 当然メソッド内でのselfはPst::ActiveRecord_Associations_CollectionProxyで posts が入っている。 # CollectionProxyに生えているので 当然 Post.first.hoge ではよべない. # posts.hoge でだけcall可能 # 引数で値を取れる。自分の値は取れないらしい。 has_many :posts do def hoge(title) p title p self.class end end
Extending
module を使って共通化できる
module Foo def foo_method end end class Category < ApplicationRecord has_many :foo, -> { extending Foo }, class_name: "Post", foreign_key: "category_id" end
自分の.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.alias_command 'e', 'edit' # open with rubymine Pry.commands.alias_command 'mi', 'edit' # open with rubymine Pry.commands.alias_command 'mine', 'edit' # open with rubymine # gem 'pry-stack_explorer' Pry.commands.alias_command 'ss', 'show-stack' Pry.commands.alias_command 'code', 'show-source' # Hit Enter to repeat last command Pry::Commands.command /^$/, "repeat last command" do command = Pry.history.to_a.last _pry_.run_command(command) unless command == "exit" # 起動直後にenter押下で即終了するのを防ぐ end Pry.config.editor = proc { |file, line| "/usr/local/bin/mine --line #{line} #{file}" } Pry.config.pager = true # プロンプト Pry.config.prompt = proc do |obj, nest_level, _pry_| prompt = "" prompt << "#{Pry.config.prompt_name.to_s.blue}" prompt << "(#{Pry.view_clip(obj).to_s.cyan})" prompt << "[#{RUBY_VERSION.to_s.green}]" prompt << "> " end # 外部gemのコードを省いた call stack def caller_products caller.reject {|l| l.include?( 'gems' )} end # 外部gemのコードのみの call stack def caller_gems caller.select {|l| l.include?( 'gems' )} end class String def red; "\e[31m#{self}\e[0m"; end def green; "\e[32m#{self}\e[0m"; end def yellow; "\e[33m#{self}\e[0m"; end def blue; "\e[34m#{self}\e[0m"; end def magenta;"\e[35m#{self}\e[0m"; end def cyan; "\e[36m#{self}\e[0m"; end def bold; "\e[1m#{self}\e[0m"; end def back_blue; "\e[44m#{self}\e[0m"; end end
Railsで使っているgemの内部で実行を止める
以下をコードの止めたいところに書けば良い。 メソッドの定義内に書いても問題ない。
require "pry"; binding.pry
コードを確認し終わって、元に戻したい時は以下を実行すれば良い。
$ bundle pristine GEM_NAME