Ruby日常

1 为什么有的时候需要使用bundle exec以及它做了什么
2 怎样在一个Ruby项目中使用bundler

1 为什么有的时候需要使用bundle exec以及它做了什么
经常会出现的问题是you have already actived rake 12.2.2, but you gemfile requires 12.1.2, prepending "bundle exec" to your command may resolve this problem

#通过which查看gem的安装地址
which rake
/Users/jayzen/.rvm/gems/ruby-2.3.0/bin/rake

使用bundle exec的原因是因为系统安装的gem和项目需要的gem是不一样的,执行bundle exec是去load项目需要gem(因为项目有gemfile文件,可以根据这个文件判断有哪些gems)

注意:最好能研究bundle这个gem的源码

2 怎样在一个Ruby项目中使用bundler

#1 建立use_bundle_demo文件夹
#2 文件夹下面执行bundle init命令,生成Gemfile文件,修改文件内容
source "https://ruby.taobao.org"
gem "activesupport"

#3 文件夹下面执行bundle install命令,生成Gemfile.lock文件
#4 文件夹下面生成root.rb和main.rb文件
#5 boot.rb
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __FILE__)
require 'bundler/setup' # set up gems listed in the Gemfile

#6 main.rb
require File.expand_path('../boot', __FILE__)
require 'active_support/all'

p 1.days

#7 终端执行ruby main.rb

你可能感兴趣的:(Ruby日常)