Skip to the content.

ruby

記法

コード ``` def natural_number?(number) return number > 0 end puts natural_number?(3) ```

bundle

コード ``` # gemのインストール bundle install --path vendor/bundle bundle update ```

エラー解消

コード ``` gem "better errors" gem "binding of caller" byebug # railsに恐らくデフォルトで入っている。import pdb;pdb.trace()のように仕込めばデバッグ可能 ```

Ruby環境構築(AWS-amazonlinux) -> 参考

1. rbenvのインストール ``` # yum更新 sudo yum update -y # gitのインストール sudo yum install git -y # rbenvをリポジトリからクローン git clone https://github.com/sstephenson/rbenv.git ~/.rbenv # クローンしてきたらrbenvのPATHを通す echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(rbenv init -)"' >> ~/.bash_profile source ~/.bash_profile # rbenvのバージョンが表示されればインストールは成功 rbenv -v ```
2. ruby-buildのインストール `※Rubyをrbenv経由でインストールする時に必要なrbenvのプラグイン。これ入ってないとRubyインストール不可` ``` # リポジトリからクローン git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build # インストールの実行 cd ~/.rbenv/plugins/ruby-build sudo ./install.sh # インストール可能なRubyのバージョン一覧が表示されればruby-buildのインストールは成功 rbenv install -l ```
3. Rubyのインストール ``` # Rubyインストールに必要なパッケージをインストール sudo yum -y install gcc-c++ glibc-headers openssl-devel readline libyaml-devel readline-devel zlib zlib-devel libffi-devel libxml2 libxslt libxml2-devel libxslt-devel sqlite-devel # rbenvでバージョンを指定してRubyをインストール rbenv install バージョン名 # rbenvで使用するRubyのバージョンを指定 rbenv global 2.7.0 # Rubyのバージョンを確認する(指定したバージョンが表示されれば成功) ruby -v ```

Rails

rails s -b 0.0.0.0
# バージョンを指定して rails new
rails _5.1.6_ new アプリ名
cd アプリ名

# javascriptがない時にエラーが起こる場合、nodejsをインストール
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node
# Gemfile
gem 'mysql2'
bundle install --path vendor/bundle
# mysql-develないとエラーになるため、インストール
sudo yum install mysql-devel
  1. 最初からmysqlを使用する際はオプションあり
rails _5.1.6_ new english_words -d mysql
# 後、config/database.ymlの接続設定を行う
rails generate scaffold Word english:string japanese:string remarks:string
rails db:create
# DBを作成する。下と同じ
>mysql
create database words;

rails db:migrate
rails db:seed
require "csv"

CSV.foreach('db/words.csv') do |info|
  Word.create(:english => info[0], :japanese => info[1], :remarks => info[2], :created_at => info[3], :updated_at => info[4])
end
  1. 初期データを反映させる
rails db:seed

railsコマンド

rails generate model モデル名 フィールド名1:データ型1 ...
rails generate controller StaticPages home help
rails destroy controller StaticPages home help
rails destroy model User
# 1つ前の状態に戻る
rails db:rollback

# 最初の状態に戻る
rails db:migrate VERSION=0
完全なコマンド 短縮形コマンド
rails server rails s
rails console rails c
rails generate rails g
rails test rails t
bundle install bundle
rails test

config/route.rbでルーティング作成 controllerでアクション追加 viewでhtmlファイル作成 ↑でtest通る

test/test_helper.rb

require "minitest/reporters"
Minitest::Reporters.use!
"#{foo} bar" # ダブルクオテーションでは式展開される
'#{foo} bar' # シングルクオテーションでは式展開されない
>> "foobar".length        # 文字列に "length" というメッセージを送る
=> 6

>> "".empty?
=> true

>> nil.to_s
=> ""

>> a = %w[foo bar baz quux]         # %wを使って文字列の配列に変換
=> ["foo", "bar", "baz", "quux"]


>> (1..5).each { |i| puts 2 * i }
2
4
6
8
10
=> 1..5

>> (1..5).each do |i|
?>   puts 2 * i
>> end
2
4
6
8
10
=> 1..5

# シンボル
user = { :name => "Michael Hartl", :email => "michael@example.com" }
{ name: "Michael Hartl", email: "michael@example.com" }


# なお、論理値はそれぞれ && (and) や || (or)、! (not) オペレーターで表すこともできます。


Railtutorial 5章

link_to
コード
unless 条件式 then
  条件式が偽の時に実行する処理
end