RubyとPostgreSQLとsinatraと(その壱)再起動せえ。
# PostgreSQLために
gem install pg
をしたいのに、エラー?
まずは、Homebrew を使って PostgreSQLをいれる。
brew update brew install postgresql
ここで、エラー
gem install pg /Users/myMBP/.rbenv/shims/gem: line 21: /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv: No such file or directory
こっちも同じく。
sinatra % bundler install /Users/myMBP/.rbenv/shims/bundler: line 21: /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv: No such file or directory sinatra %
エラー箇所に潜ってみる。
code /Users/myMBP/.rbenv/shims/gem
21: exec "/usr/local/Cellar/rbenv/1.1.2/libexec/rbenv" exec "$program" "$@"
おーおったわー
rbenv/1.1.2のバージョン違いっぽい
じゃあその参照先を確認っと、
こいつに書き換える。
これで試す。
エラー。。。
これか?と思って試そうとするも、そもそもプロキシとか設定とかしてないし、理解浅いしで積んでました。
最終的に
再起動
という魔法で解決しました。
sinatra % gem install pg Fetching pg-1.2.3.gem Building native extensions. This could take a while... Successfully installed pg-1.2.3 Parsing documentation for pg-1.2.3 Installing ri documentation for pg-1.2.3 Done installing documentation for pg after 1 seconds 1 gem installed
うーんこの・・・
納得はいかんが、次に進めるのでよしとする。
困ったら、再起動はほんとあるあるよなあ。
式展開のある記述では、”” ダブルクオーテーションを使え。
# 問題です。
どちらが正しいでしょうか?
A:
redirect to("/memos/#{params[:id]}")
B:
redirect to(‘/memos/#{params[:id]}’)
答えはCMのあとで。
# 答えの時間
⭕️:
```rb
redirect to("/memos/#{params[:id]}")
```
❌:
```rb
redirect to('/memos/#{params[:id]}')
```
# なぜシングルクォーテーションではだめなのか
例えば、
```rb
redirect to('/memos')
```
はルーティング問題ありませんでした。
これの上との違いといえば、式展開してますね。
ということで、確認。
[リファレンス](https://docs.ruby-lang.org/ja/latest/doc/spec=2fliteral.html)
> 文字列リテラル
例:
"this is a string expression\n"
'this is a string expression\n'
%q!I said, "You said, 'She said it.'"!
%!I said, "You said, 'She said it.'"!
%Q('This is it.'\n)
"this is multi line
string"
文字列はダブルクォートまたはシングルクォートで囲まれています。ダブルクォートで囲まれた文字列ではバックスラッシュ記法と式展開(後述)が有効になります。シングルクォートで囲まれた文字列では、 \\ (バックスラッシュそのもの)と \' (シングルクォート) を除いて文字列の中身の解釈は行われません。シングルクォートで囲まれた文字列では行末の \ は \ そのものとして解釈されます。
複数行にわたって書くこともできます。この場合含まれる改行文字は常に\nになります。実際のソースファイルの改行コードとは無関係です。
空白を間に挟んだ文字列リテラルは、コンパイル時に1つの文字列リテラルと見倣されます。
らしいです。
ちなみに↓
# 参考記事
https://qiita.com/ryosuketter/items/ddad508cb0124e4fe378
https://necojackarc.hatenablog.com/entry/2015/09/12/220832
今後、””派になろうかと思います。
以上です。
sinatraのエスケープ処理について
helpers do
def h(text)
Rack::Utils.escape_html(text)
end
end
erbなら以下.<%= h "" %>
ruby sinatra json 困ったらこれをみろ。のリンク集
初歩:
中:
rightgo09-ruby.hatenadiary.org
リファレンス:
get '/memos' do
File.open('storage/*', "r") do |file|
@hash = JSON.parse(file.read, symbolize_names: true)
end
erb :index
end
メモ
<%= @hash[:title] %><%= @hash[:content] %>/*_________________*/
get '/memos' do
json = Dir.glob('storage/*').sort_by { |file| File.birthtime(file) }
@memos = json.map { |file| JSON.parse(File.read(file)) }
erb :index
end
メモ
<%= @hash['title'] %><%= @hash['content'] %>HTMLの記述
【sinatra】bundle execコマンド実行時にエラー Server handler (thin,puma,reel,HTTP,webrick) not found
sinatra % bundle exec ruby app.rb
/Users/myMBP/work/FJORD/sinatra/vendor/bundle/ruby/3.0.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1755:in `detect_rack_handler': Server handler (thin,puma,reel,HTTP,webrick) not found. (RuntimeError)
from /Users/myMBP/work/FJORD/sinatra/vendor/bundle/ruby/3.0.0/gems/sinatra-2.1.0/lib/sinatra/base.rb:1493:in `run!'
from /Users/myMBP/work/FJORD/sinatra/vendor/bundle/ruby/3.0.0/gems/sinatra-2.1.0/lib/sinatra/main.rb:45:in `block in
sinatra %
上記のエラーが出ました。
ググった記事では、まあ足りないの全部入れれば?となっています。
そんなわけにもいかないので、
こちらによると、3.0から
> 以下のライブラリは標準添付ライブラリから削除されました。3.0 以降で使いたい場合は rubygems から利用してください。
sdbm
webrick
net-telnet
xmlrpc
とのこと。
なので、インストールします。
Fetching webrick-1.7.0.gem
Successfully installed webrick-1.7.0
Parsing documentation for webrick-1.7.0
Installing ri documentation for webrick-1.7.0
Done installing documentation for webrick after 0 seconds
1 gem installed
Gemfileに下記も記入。
gem 'webrick'
忘れないようにすること!
bundle install コマンドの実行。
(そうでないと番外編みたく彷徨うことになります。)
実行!
sinatra % bundle exec ruby app.rb
[2021-10-09 22:12:35] INFO WEBrick 1.7.0
[2021-10-09 22:12:35] INFO ruby 3.0.2 (2021-07-07) [x86_64-darwin20]
== Sinatra (v2.1.0) has taken the stage on 4567 for development with backup from WEBrick
[2021-10-09 22:12:35] INFO WEBrick::HTTPServer#start: pid=74232 port=4567
これで成功です。
番外編
さまよった軌跡。
sinatra % bundle exec ruby app.rb
Could not find gem 'webrick' in rubygems repository https://rubygems.org/ or installed locally.
The source does not contain any versions of 'webrick'
Run `bundle install` to install missing gems.
sinatra %
あれれ〜?
伊藤さんの記事から
sinatra % bundle update webrick
Could not find gem 'webrick'.
を叩くも不発。
bundle add gem webrick
あ、そういえばと上のコマンドを叩く。
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Using bundler 2.2.22
Using multi_json 1.15.0
Using ruby2_keywords 0.0.5
Using rack 2.2.3
Using mustermann 1.1.1
Fetching webrick 1.7.0
Using rack-protection 2.1.0
Using tilt 2.0.10
Fetching gem 0.0.1.alpha
Using sinatra 2.1.0
Using sinatra-contrib 2.1.0
Installing webrick 1.7.0
Installing gem 0.0.1.alpha
そしてUpdateする。
sinatra % bundle update webrick
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Using bundler 2.2.22
Using gem 0.0.1.alpha
Using tilt 2.0.10
Using ruby2_keywords 0.0.5
Using rack 2.2.3
Using mustermann 1.1.1
Using multi_json 1.15.0
Using rack-protection 2.1.0
Using sinatra 2.1.0
Using sinatra-contrib 2.1.0
Bundle updated!
Gemファイルに下記が追加された。
gem "gem", "~> 0.0.1.alpha"
ただしこれは、
gem 'webrick'
と同じである。
ここでようやく、
bundle install
で設定が反映されることに気づく。
重複した、
gem "gem", "~> 0.0.1.alpha"
は削除した。
zshにはtreeコマンドがない。
対策:
brew install tree
Homebrewでインストールすること。
sinatra 最初の一歩
はじめに
現在sinatraでの簡易メモアプリを作成中です。
当記事では、ブラウザにハローワールドするまでをメモしておきます。
参考記事:
bundle init を実行すると、Gemファイルができます。
bundle install時に--path vendor/bundleを付ける必要性は本当にあるのか、もう一度よく考えてみよう
ここで注意点!
伊藤さんの記事から、
参考記事での--path vendor/bundle の記述をしないことにします。
bundle install
を実行します。
sinatra % tree
.
├── Gemfile
├── Gemfile.lock
├── README.md
├── app.rb ★こここここ
├── vendor
│ └── bundle
│ └── ruby
│ └── 3.0.0
│ ├── bin
└── views
sinatra % ll
total 32
Gemfile
Gemfile.lock
README.md
app.rb
vendor
views
./app.rb
記述する。
require 'sinatra'
get '/' do
'hello world'
end
実行コマンド
bundle exec ruby app.rb