Ruby小白入門筆記之


      因為初學Ruby,四處查資料無果,才來的貼出親自試過的操作,覆蓋整個個人入門筆記博客中,故所有的操作,都以最明了的方式闡述,當你創建完一個新的Rails應用后,你發現JAVA中我們可以編寫maven聚合項目來控制其子項目的JDK,TOMCAT等等的版本,那么Ruby是怎樣來管理的呢,答案就是在Gemfile中加依賴,而這個文件不是你手動編寫的,而是使用Bundler來安裝和引入該應用所需的gem。執行rails new命令時會自動運行Bundler(bundle install命令),而bundle就是根據Gemfile文件中的來依賴你所定義的源,就像JAVA中的依賴jar包一樣。下面先看看默認生成的Gemfile中有什么?

 1 source 'https://rubygems.org'
 2 git_source(:github) { |repo| "https://github.com/#{repo}.git" }
 3 
 4 ruby '2.3.1'
 5 
 6 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 7 gem 'rails', '~> 5.2.0'
 8 # Use sqlite3 as the database for Active Record
 9 gem 'sqlite3'
10 # Use Puma as the app server
11 gem 'puma', '~> 3.11'
12 # Use SCSS for stylesheets
13 gem 'sass-rails', '~> 5.0'
14 # Use Uglifier as compressor for JavaScript assets
15 gem 'uglifier', '>= 1.3.0'
16 # See https://github.com/rails/execjs#readme for more supported runtimes
17 # gem 'mini_racer', platforms: :ruby
18 
19 # Use CoffeeScript for .coffee assets and views
20 gem 'coffee-rails', '~> 4.2'
21 # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
22 gem 'turbolinks', '~> 5'
23 # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
24 gem 'jbuilder', '~> 2.5'
25 # Use Redis adapter to run Action Cable in production
26 # gem 'redis', '~> 4.0'
27 # Use ActiveModel has_secure_password
28 # gem 'bcrypt', '~> 3.1.7'
29 
30 # Use ActiveStorage variant
31 # gem 'mini_magick', '~> 4.8'
32 
33 # Use Capistrano for deployment
34 # gem 'capistrano-rails', group: :development
35 
36 # Reduces boot times through caching; required in config/boot.rb
37 gem 'bootsnap', '>= 1.1.0', require: false
38 
39 group :development, :test do
40   # Call 'byebug' anywhere in the code to stop execution and get a debugger console
41   gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
42 end
43 
44 group :development do
45   # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
46   gem 'web-console', '>= 3.3.0'
47   gem 'listen', '>= 3.0.5', '< 3.2'
48   # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
49   gem 'spring'
50   gem 'spring-watcher-listen', '~> 2.0.0'
51 end
52 
53 group :test do
54   # Adds support for Capybara system testing and selenium driver
55   gem 'capybara', '>= 2.15', '< 4.0'
56   gem 'selenium-webdriver'
57   # Easy installation and use of chromedriver to run system tests with Chrome
58   gem 'chromedriver-helper'
59 end
60 
61 # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
62 gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

  可以看到很多的代碼都被#注釋掉了,這些代碼之所以注釋而又放在這,是為了告訴你一些常用的gem,也是為了展示Bundler的句法,在gem指令中,如果你沒有指定版本,那么Bundler就會默認安裝最新版。

題外話:   

gem 'uglifier', '>= 1.3.0'
這行代碼的意思是,安裝版本號大於或等於1.3.0的uglifier(作用是壓縮 Asset Pipeline中的文件),就算是7.2版也會安裝。
gem 'coffee-rails','~> 4.0.0'
這行代碼的意思是,安裝版本號大於4.0.0,但小於4.1的coffee-rails。也就是說,>=表示法意思是始終安裝最新版;~> 4.0.0表示法的意思是只安裝最后一個數字變化的版本

   但是往往開發中我們會修改Gemfile,換用更為精准的版本號,並且引用國內淘寶的,就不用翻牆,提高速度,那么下面就來看看修改過后的文件。

        注意將source 'https://rubygems.org'  改為   source 'https://ruby.taobao.org'

修改完后執行Bundle install命令,來安裝這些gem,注意別像我一樣蠢,一開始居然沒在項目內運行命令,應該在你帶有Gemfile的目錄下執行Bbundle install.然后出現類似下面提示,就是更新依賴成功

Using rake 12.3.1
Using concurrent-ruby 1.0.5
Using i18n 1.0.1
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.2.0
Using builder 3.2.3
Using erubi 1.7.1
Using mini_portile2 2.3.0
Using nokogiri 1.8.2
Using rails-dom-testing 2.0.3
Using crass 1.0.4
Using loofah 2.2.2
Using rails-html-sanitizer 1.0.4
Using actionview 5.2.0
Using rack 2.0.5
Using rack-test 1.0.0
Using actionpack 5.2.0
Using nio4r 2.3.1
Using websocket-extensions 0.1.3
Using websocket-driver 0.7.0
Using actioncable 5.2.0
Using globalid 0.4.1
Using activejob 5.2.0
Using mini_mime 1.0.0
Using mail 2.7.0
Using actionmailer 5.2.0
Using activemodel 5.2.0
Using arel 9.0.0
Using activerecord 5.2.0
Using mimemagic 0.3.2
Using marcel 0.3.2
Using activestorage 5.2.0
Using public_suffix 3.0.2
Using addressable 2.5.2
Using io-like 0.3.0
Using archive-zip 0.11.0
Using bindex 0.5.0
Using msgpack 1.2.4
Using bootsnap 1.3.0
Using bundler 1.16.2
Using byebug 10.0.2
Using xpath 3.1.0
Using capybara 3.2.1
Using ffi 1.9.25
Using childprocess 0.9.0
Using chromedriver-helper 1.2.0
Using coffee-script-source 1.12.2
Using execjs 2.7.0
Using coffee-script 2.4.1
Using method_source 0.9.0
Using thor 0.20.0
Using railties 5.2.0
Using coffee-rails 4.2.2
Using multi_json 1.13.1
Using jbuilder 2.7.0
Using rb-fsevent 0.10.3
Using rb-inotify 0.9.10
Using ruby_dep 1.5.0
Using listen 3.1.5
Using puma 3.11.4
Using sprockets 3.7.1
Using sprockets-rails 3.2.1
Using rails 5.2.0
Using rubyzip 1.2.1
Using sass-listen 4.0.0
Using sass 3.5.6
Using tilt 2.0.8
Using sass-rails 5.0.7
Using selenium-webdriver 3.12.0
Using spring 2.0.2
Using spring-watcher-listen 2.0.1
Using sqlite3 1.3.13
Using turbolinks-source 5.1.0
Using turbolinks 5.1.1
Using uglifier 4.1.11
Using web-console 3.6.2
Bundle complete! 18 Gemfile dependencies, 78 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

 

好了,到此為止Gemfile文件就入門完了,嘻嘻。

如果你還想深入了解Gemfile文件,那個可以參考下  http://tosbourn.com/what-is-the-gemfile/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM