Skip to main content

2 posts tagged with "rails"

View All Tags

· 2 min read
junminhong(jasper)

nil?

  • provided by ruby

  • 可以使用在任何型別上

  • 只有nil才會return true, 否則return false

# 只有nil才會回傳true
nil.nil? => true

[].nil? => false
{}.nil? => false
0.nil? => false
false.nil? => false
"".nil? => false
" ".nil? => false

empty?

  • provided by ruby

  • 僅用於 Hash、Array、Set、String

  • 當沒有任何元素時才會return true, 否則return false

當你將empty? 使用在沒有該方法的類別上, 會得到NoMethodErrorexception, 所以你會需要使用檢查來避免程式在運行上不會崩潰

[].empty? => true
{}.empty? => true
Set.new.empty? => true
# 當用於string時, 可把他想像成bytes/characters的集合
"".empty? => true

" ".empty? => false

blank?

  • provided by rails(in ActiveSupport)

  • 可以使用在任何型別上

  • 任何false相關的情況都會回傳true, (如nil and false)

如同你所看到的, blank?empty? 更好用, 可以避免無法使用的資料處理上, 會回傳NoMethodErrors 錯誤

[].blank? => true
{}.blank? => true
Set.new.blank? => true
"".blank? => true
" ".blank? => true
false.blank? => true
nil.blank? => true

# 如果是Hash、Array、Set, 原理跟empty一樣, 如果沒有元素就回傳true
[nil].blank? => false
["", ""].blank? => false
{name: 'jasper'}.blank? => false
true.blank? => false

present?

  • provided by rails

  • blank?相反


true.present? => true
{name: 'jasper'}.present? => true
3.present? => true

false.present? => false
{}.present? => false
"".present? => false
" ".presnet? => false
[].present? => false
nil.present? => false

· 4 min read
junminhong(jasper)

前言

最近在進行rails專案bundle install的時候, 發現有一個gem始終裝不起來。

這邊記錄一下排查思路以及解決方式

進入排查程序

當在做bundle install的時候遇到了這個錯誤訊息 mini-racer-bundle-error

於是乎呢!我就直接使用google大法把這段錯誤訊息貼上查詢, 不外乎就會得到一些解答

這邊列舉幾個issue有在討論這件事情

從幾個討論串中得知了幾種做法

第一種做法

主要是透過自行安裝v8並在bundle的時候指定使用剛剛安裝好的v8

# 透過homebrew安裝v8
brew install v8
# 然後指定bundle config
bundle config build.libv8 --with-system-v8

bundle install

第二種做法

# 新增一個bundle lock 為ruby環境
bundle lock --add-platform ruby

以上做法都無法解決我的問題, 但由於呢始終沒有頭緒, 所以我只好重新安裝ruby

這邊我來介紹一下怎麼透過rbenv管理ruby

安裝好用的ruby管理器rbenv

# 透過brew安裝
brew install rbenv
# 安裝指定ruby版本
rbenv install 2.7.4
# 記得安裝一個新的ruby版本一定要執行這個指令
rbenv rehash

這樣就完成的ruby的安裝

繼續排查之路

當我重新安裝好ruby並重新bundle install的時候又發現還是出現一樣的問題

實在是走投無路的我只好乖乖的把bundle error log拿出來看看, 找看看有沒有什麼蛛絲馬跡

結果!似乎讓我找到了點東西

# 這邊有發現幾行的錯誤訊息, 看起來似乎跟macos自身帶的commandLineTools有關
No receipt for 'com.apple.pkg.CLTools_Executables' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLILeo' found at '/'.

No receipt for 'com.apple.pkg.DeveloperToolsCLI' found at '/'.

Node.js configure: Found Python 3.9.13...
WARNING: --openssl-no-asm will result in binaries that do not take advantage
of modern CPU cryptographic instructions and will therefore be slower.
Please refer to BUILDING.md
WARNING: warnings were emitted in the configure phase

當我看到了這個錯誤訊息後, 我就去試著找了一下macos本身的commandLineTools相關的資訊

接著我就試著重裝一下xcode commandLineTools, 果真就可以正常安裝了

macos xcode commandLineTools 怎麼重新安裝

xcode-select -print-path
# 你會找到你的xcode commandLineTools裝在哪裡
# /Library/Developer/CommandLineTools

# 移除xcode commandLineTools
sudo rm -r /Library/Developer/CommandLineTools

# 重新安裝xcode commandLineTools
xcode-select --install

# Switch Xcode's path
sudo xcode-select -switch /Library/Developer/CommandLineTools

# Reset Xcode's path
sudo xcode-select --reset