认识 Ruby 与 Gem

作者: _涼城 | 来源:发表于2022-04-22 12:02 被阅读0次

    什么是 Ruby

    Ruby

        Ruby 是一种面向对象、指令式、函数式、动态的通用编程语言。Mac 内部默认是有 Ruby 的,其路径可以通过 which ruby获取,默认为/usr/bin/ruby,可以通过 ruby -v 命令查看当前的版本。

    RVM

        有些时候系统默认的 Ruby 版本满足不了我们的需求,就会使用到 RVMRVM,全名 Ruby Version Manager,是一个命令行工具,可安装、管理和使用多个 Ruby 环境。

    RVM 安装方式

    rvm 有两种安装方式

    • 官网脚本安装

        \curl -sSL https://get.rvm.io | bash -s
      

          rvm 会安装到当前用户目录下也就是 ~/,路径为 ~/.rvm,安装后需要运行 source ~/.rvm/scripts/rvm

    • 离线安装

      1. 克隆仓库

        git clone https://github.com/rvm/rvm.git
        
      2. 运行 rvm/bin/ 路径下的 rvm-installer

      3. 配置环境变量

        export PATH="$HOME/.rvm/bin:$PATH"
        

    RVM 的使用

    • 安装指定版本的 Ruby (需要先安装 Homebrew)

       #安装后路径为 ~/.rvm/rubies/ruby-x.x.x
       rvm install 2.7
      
    • 指定 Ruby 的默认版本

      rvm use 2.7 --default
      

    RVM 的移除

    rvm implode
    

    Gem

         GemRuby 中的包,其中包含包信息,以及用于安装的文件。Gem 通常是依照 .gemspec 文件构建的,其为 YAML 文件。然而,Ruby 代码也可以直接创建 Gem,这种情况下通常利用Rake 来进行。

    .gemspec

        .gemspec 文件其中包含 Gem 有关的信息,例如版本号、作者、联系邮件以及依赖等,例如 cocoapods.gemspec 文件如下:

    # encoding: UTF-8
    require File.expand_path('../lib/cocoapods/gem_version', __FILE__)
    require 'date'
    
    Gem::Specification.new do |s|
      s.name     = "cocoapods"
      s.version  = Pod::VERSION
      s.date     = Date.today
      s.license  = "MIT"
      s.email    = ["eloy.de.enige@gmail.com", "fabiopelosin@gmail.com", "kyle@fuller.li", "segiddins@segiddins.me"]
      s.homepage = "https://github.com/CocoaPods/CocoaPods"
      s.authors  = ["Eloy Duran", "Fabio Pelosin", "Kyle Fuller", "Samuel Giddins"]
    
      s.summary     = "The Cocoa library package manager."
      s.description = "CocoaPods manages library dependencies for your Xcode project.\n\n"     \
                      "You specify the dependencies for your project in one easy text file. "  \
                      "CocoaPods resolves dependencies between libraries, fetches source "     \
                      "code for the dependencies, and creates and maintains an Xcode "         \
                      "workspace to build your project.\n\n"                                   \
                      "Ultimately, the goal is to improve discoverability of, and engagement " \
                      "in, third party open-source libraries, by creating a more centralized " \
                      "ecosystem."
    
      s.files = Dir["lib/**/*.rb"] + %w{ bin/pod bin/sandbox-pod README.md LICENSE CHANGELOG.md }
    
      s.executables   = %w{ pod sandbox-pod }
      s.require_paths = %w{ lib }
    
      # Link with the version of CocoaPods-Core
      s.add_runtime_dependency 'cocoapods-core',        "= #{Pod::VERSION}"
    
      s.add_runtime_dependency 'claide',                '>= 1.0.2', '< 2.0'
      s.add_runtime_dependency 'cocoapods-deintegrate', '>= 1.0.3', '< 2.0'
      s.add_runtime_dependency 'cocoapods-downloader',  '>= 1.6.0', '< 2.0'
      s.add_runtime_dependency 'cocoapods-plugins',     '>= 1.0.0', '< 2.0'
      s.add_runtime_dependency 'cocoapods-search',      '>= 1.0.0', '< 2.0'
      s.add_runtime_dependency 'cocoapods-trunk',       '>= 1.6.0', '< 2.0'
      s.add_runtime_dependency 'cocoapods-try',         '>= 1.1.0', '< 2.0'
      s.add_runtime_dependency 'molinillo',             '~> 0.8.0'
      s.add_runtime_dependency 'xcodeproj',             '>= 1.21.0', '< 2.0'
    
      s.add_runtime_dependency 'colored2',       '~> 3.1'
      s.add_runtime_dependency 'escape',        '~> 0.0.4'
      s.add_runtime_dependency 'fourflusher',   '>= 2.3.0', '< 3.0'
      s.add_runtime_dependency 'gh_inspector',  '~> 1.0'
      s.add_runtime_dependency 'nap',           '~> 1.0'
      s.add_runtime_dependency 'ruby-macho',    '>= 2.3.0', '< 3.0'
    
      s.add_runtime_dependency 'addressable', '~> 2.8'
    
      s.add_development_dependency 'bacon', '~> 1.1'
      s.add_development_dependency 'bundler', '~> 2.0'
      s.add_development_dependency 'rake', '~> 12.3'
    
      s.required_ruby_version = '>= 2.6'
    

    什么是 RubyGems

        RubyGems 是 Ruby 的一个包管理器源,提供了分发 Ruby 程序和库的标准格式 gem,旨在方便地管理 gem 安装的工具,以及用于分发 gem服务器源。这类似于 Pythonpip

    Gem 包的安装和卸载

    gem 命令文档

    • 使用系统 Ruby 环境,一般需要管理员权限,会默认安装到 /Library/Ruby/Gems/x.x.x

      sudo gem install  xxx
      
    • 使用 rvm ruby 环境,默认会安装在 ~/.rvm/gems/ruby-2.6.8

      gem install xxx
      
    • 使用 --user-install ,默认会安装在 ~/.gem/ruby 下,需要提前配置环境变量

      PATH="`ruby -e 'puts Gem.user_dir'`/bin:$PATH"
      gem install xxx --user-install
      
    • 查看已经安装的依赖包

      gem list
      
    • 卸载依赖包

       gem uninstall xxx
      

    GEM_HOME & GEM_PATH

        一般地,可以通过 gem env 查看 gem 的位置 GEM_HOME 和其执行路径 GEM_PATH,除了上述外可以通过覆写 GEM_HOME & GEM_PATH 从而不需要管理员权限(sudo)。

    export GEM_HOME=$HOME/.gem
    export PATH=$GEM_HOME/bin:$PATH
    

    GEM 源

        gem 命令行的默认源是 RubyGems,也就是 https://rubygems.org/,以下是对源的操作

    #查看源
    gem source -l 
    #镜像源
    gem source -a https://gems.ruby-china.com/  
    #移除源
    gem source --remove https://rubygems.org/
    

    Bundler

    Bundler 能够跟踪并安装所需的特定版本的 gem,以此来为 Ruby 项目提供一致的运行环境。定义一个 Gemfile,说明想要包含哪些库,并且可以选择指定版本或范围。 运行bundle install,它会生成一个 Gemfile.lock,说明所有库的确切版本,然后bundle install使用该项目运行的任何其他人都会获得完全相同的版本。

    安装

    gem install bundler
    

    使用

    在项目根目录下新建 Gemfile 文件并指定所需的依赖:

    source 'https://gems.ruby-china.com/'
    gem 'fastlane'
    

    配置后使用 bundle install 安装

    执行

    bundle exec pod [command]
    

    相关文章

      网友评论

        本文标题:认识 Ruby 与 Gem

        本文链接:https://www.haomeiwen.com/subject/patnertx.html