雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

ruby

undefined method `[]' for Matrix:Module

railsや自作gemを作成するときにMatrixを使うときの注意。 エラー内容 タイトル通り undefined method `[]' for Matrix:Module 原因 Gemfileに gem 'matrix' と書いてbundle install してしまうとタイトルのようなエラーが出る模様。(matrix 0.0.1っていう…

rubyで行列の積を求める

やりたいこと 以下の行列の積を求めたい時 \begin{equation} a = \left[ \begin{array}{r} -1 & 20 & 3 \end{array} \right] \\ \end{equation} \begin{equation} b= \left[ \begin{array}{rrr} -1 \\ 4 \\ 7 \end{array} \right] \end{equation} \begin{equ…

arrayの各要素をindexを参照しながら、足していく場合のコード

rubyで「arrayの各要素のindexを参照しながら、足す」という操作をしたかったので、メモ 以下、参考コード array = [1, 2, 3] sum = array.each_with_index.inject(0.0){|sum, (d, i)| p "i:#{i}" # index sum += d } p "sum = #{sum}" 以下、出力 "i:0" "i:…

lambda式メモ

lambda式 pythonで lambda 引数:処理 hello = lambda who : print(who + "hello!") hello("inoki") # => inoki hello! rubyで = -> (引数){処理} ()は省略可能 hello = -> (who){p who+" hello!"} hello["inoki"] # => inoki hello! パーフェクトRuby (PERFE…

rubyのMechanizeでダウンロード

rubyのMechanizeでダウンロード rubyのmechanizeを使ってファイルをダウンロードしたいとき コード require "mechanize" agent = Mechanize.new agent.pluggable_parser.default = Mechanize::Download agent.get(<url>).save(<filename>) # <> はすべて自分用に変更してく</filename></url>…

mechanizeのlinks_withの使い方メモ

mechanizeのlinks_withの使い方メモ linksの中から特定の文字列が含まれるlinkだけ取りたい時 require "mechanize" agent = Mechanize.new a = agent.get("http://yahoo.co.jp") a.links_with(text: /yahoo/)

pryでshellコマンドを実行する方法

pryでshellコマンドを実行する方法 ドット(.)でshell コマンドが使える 例 .ls .cd

rails libに格納してある自作ライブラリを使用したいとき

rails libに格納してある自作ライブラリを使用したいとき config/envrioment.rb require "lib" lib/lib.rb module Lib def self.test end end app/controllersの中で、そのまま以下のように使用できる Lib.test

gem build時のエラー

gem build時のエラー gem buildコマンドを実行した時に以下のようなエラーをはいた rake aborted! ERROR: While executing gem ... (Gem::InvalidSpecificationException) "FIXME" or "TODO" is not an author Tasks: TOP => build (See full trace by runni…

railsでローカルファイルを読み込みたい、参照したい

railsでローカルファイルを読み込みたい、参照したい 以下のように設定する "#{Rails.root}/app/controllers/word_set/word_list_test.yml"

herokuでmecabを使用するときの注意

herokuでmecabを使用するときの注意 Gemfileに以下を追記 gem 'mecab', '0.996' 以下のコマンド実行 bundle install git add . git commit . その後に以下のコマンドを実行しないとダメ. heroku create アプリ名 heroku config:set BUILDPACK_URL=https://gi…

アプリの削除コマンド

アプリの削除コマンド heroku apps:destroy --app アプリ名

自作のgemの使い方メモ

自作のgemの使い方メモ これは特に自分用 require "gem_name" include GemName require "gem_name" include GemName:GemTool など

gem 変更時のエラー are not files rake build

gem 変更時のエラー are not files rake build 解決法 git add . git commit -a -m "何かしらコメント" の後に rake build

gem update法

gem update法 gemをupdateするとき rake build

Railsに自作libの使用方法

Railsに自作libの使用方法 config/environment.rbに追記 require "lib" lib/lib.rb class Lib def self.test pust "test" end end 使い方はcontrollerで Lib.test

Railsに自作libの追加方法

Railsに自作libの追加方法 config/application.rbに追記 ・・・ module アプリ名 class Application < Rails::Application config.autoload_paths += %W(#{config.root}/lib) end end ・・・ lib/foo_hoge.rb class FooHoge def test pust "test" end end 実…

Railsにrspecをインストールして、簡単にテスト

Railsにrspecをインストールして、簡単にテスト Gemfileに以下を追記 group :test do gem 'rspec-rails', '~> 3.0.0' gem 'factory_girl_rails', '~> 3.0' end その後に bundle install bundle exec rails generate rspec:install 後はrspec以下にテストを記…

rubyでslackbotの作り方メモ

slackbotの作り方メモ まず、botの登録。以下手順 左上にある自分のアイコンらへんををクリック 「Profile & account」をクリック 画面右に出る項目の中で「Account Settings」をクリック 左上の「Menu」をクリック 「Configure Apps」をクリック 左のManage…

gem名.gemspecの注意点

lib/gem名/ 以下のファイルを読み込むときには,gem名.gemspecに spec.files = [`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) },追加したいパス] 具体例) spec.files = [`git ls-files -z`.split("\x0").reject { |f| f…

clockworkでnokogiriがうまく認識されない時の対処法

以下のようなエラー /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- nokogiri (LoadError) 対処法 以下をインストールするのを忘れていたらしい sudo apt-get install ruby-nokogiri

rbenvのインストールの仕方

rbenvのインストールの手順についてのメモ 以下端末にての操作 sudo apt-get install rbenv cd vim .bashrc .bashrcに export PATH="~/.rbenv/bin:$PATH" eval "$(rbenv init -)" 追加して終了。 端末にて rbenv -v と入力 「rbenv 〜」と表示されればok

open-uriのopenメソッドに日本語を用いたらエラーが出たので対処法をメモ

open-uriのopenメソッドに日本語を用いたら url= "http://search.hatena.ne.jp/keyword?word=野球&mode=rss&ie=utf8&page=1" open(url) 以下の用なエラーを吐いたので、対処法をメモ /usr/lib/ruby/1.9.1/uri/common.rb:176:in `split': bad URI(is not URI?…

3Dグラフ用のサンプルプログラム

rubyのgnuplotで3Dグラフを生成するサンプルを貼っておきます。 require "gnuplot" Gnuplot.open do |gp| Gnuplot::SPlot.new( gp ) do |plot| plot.output "test.eps" plot.xlabel "x" plot.ylabel "y" plot.zlabel "z" plot.set 'terminal postscript 16 e…

ハッシュのkeyやvalueをそれぞれ配列に保存する方法

非常に便利なメソッドがついているので簡単です。 hash = {:key1 => value1,:key2 => value2} hash.keys # => [:key1,:key2] keyを配列として取得できる hash.values # => [value1,value2] valueを配列として取得できる

render

railsでrenderを使用したのでメモ。 あるcontrollerから、そのcontrollerの名前と異なるhtmlを表示する方法 render :action => 'index' で、どのコントローラーからもindex.html.erbを開くことができる。 詳しくはリファレンスを参考にしてください。 これは…

配列を逆順に扱う方法

配列の要素分だけブロックを実行し、配列の要素を逆順に取り出す方法を紹介。 以下、サンプルプログラム array = [1,2,3] array.reverse_each do |e| puts e end 実行結果 3 2 1