Now I want in develop in local machine using gem source:
source "https://gems.ruby-china.com"
when building in github actions using gem source:
source "https://rubygems.org/"
what should I do using conditaional gem sources?
You can use an environment variable to determine which domain to use.
domain = ENV['RUBY_GEMS_CHINA'] ? 'https://gems.ruby-china.com' : 'https://rubygems.org'
source domain
You can then use RUBY_GEMS_CHINA=1 bundle locally to install the dependencies from Ruby Gems China.
Related
I want to setup gem from rubygems.org to some subfolder of my project like "/gems" and then use it from script via require. Please help me resolve it.
The preferred way is to go with a standard scenario: you build a gem, with binaries, pack it and when used it will download all the dependencies itself, according to gemspec. If the above is for some reason non-acceptable for you, one of the options would be:
• You create a gem for your script;
• You specify a path where to install dependencies:
bundle install --path=vendor/gems
• Instead of require you use require_relative in your script because on destination machines there won’t be local bundle’s config file, pointing to the right folder;
• As your script is finished, you pack everything including gems.
The normal way to go in a ruby project is to setup a Gemfile and use bundler (see link for more info) to handle the gems your project requires, without having to think of where they are stored
However, if there really is no way around shipping an own gem-directory, e.g. because your productive system has absolutely no way to access the internet, you could make it this way:
Create a directory in the root of your project (e.g. 'gems').
Download and unpack the gems to that directory (or use the appropriate options of gem install to redirect the installation into that directory)
Create a ruby file in the root directory of your project with following constants:
PROJECT_DIR = __dir__
GEMS_DIR = File.join(PROJECT_DIR, 'gems')
Now you can require your gems link require File.join(GEMS_DIR, <gem_name>)
Nonetheless, you should really think about using bundler, if possible in any way.
EDIT: to install gem via gem install
Delete the PG data from your project's gems directory
uninstall global gem gem uninstall pg
Install pg again, but into your project's directory: gem install -i <path_to_projects_gems_dir> pg
Run you script again with the require pointing to the gem stated above
I solved it like this:
gem install -i ./ pg
ROOT = File.expand_path('..', __FILE__)
ENV['GEM_PATH'] = File.join(ROOT, './')
require 'pg'
puts 'Version of libpg: ' + PG.library_version.to_s
I'm writing some scripts to set up development environment for a ruby app.
In my Gemfile, I have gems dependent on nokogiri, libv8 etc.
On running bundle install on different machines, it fails with messages like following
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
I now have this in my script to build to build native extension using system libraries.
bundle config build.nokogiri --use-system-libraries
bundle install
I can set configurations in bundler for each gem (bcrypt-ruby, libv8 etc) a similar way.
Is there a better way to do this? Like set a flag in bundler so that bundle understand details like using system libraries and bundle install works on all platforms
bundle config by default acts as global config for machine you run it - stores data in ~/.bundle/config
You may try to use --local, which stores in your_app_dir/.bundle/config and then commit the file or create it on deploy. I'd go with the latter
For a project I'm working on, I have to do some work on an existing Ruby Gem. I haven't got much previous experience with Ruby and am struggling with a few aspects.
I have the repository cloned locally. I need to edit the current files in the /lib/ directory, which I can do. Is it possible for me to compile the edited files into a Gem and run this Gem? Any help would be really appreciated.
Yes! You can include the gem in a local project and point the Gemfile to your local directory:
Gemfile
source 'https://rubygems.org'
gem 'my_local_gem', path: '/absolute/path/to/your/gem'
Then edit the gem, run your local program and see the changes.
Additionally, you don't always have to clone a whole gem, you can install it via rubygems and use bundle open <gem_name> to open the gem's contents in your supplied editor.
I have written a post describing just that and more here.
I'm not new to programming, but brand new to Ruby. Everything's working, but I'm still missing a key concept: how do you install a plugin and where/how do you include it in an app?
Example:
I'm trying to use the Facebooker2 plugin: https://github.com/mmangino/facebooker2. In the readme, step 1 is to "Install facebooker2 as a plugin in your rails app." I've run the command git clone https://github.com/mmangino/facebooker2.git to download a read only version of the repository.
Do I then bundle that up using Bundler, or do I need to create a gem file in some way? Do I simply
use gem to install it, or do I need to compile it into a gem?
Any help (terminal commands or otherwise) are extremely helpful.
I looked at the repo and it's set up as a gem. You can simply add
gem 'facebooker2'
to your Gemfile (in the root of your project) and run
bundle install
to download it and add it to your list of installed gems, both in development and in production.
Rails used to include the concept of plugins (added to your /vendor/plugins directory) but that's been dropped in favor of gems.
If you're source is source 'https://rubygems.org' but the gem you need is specific to github and not part of the rubygems.org library, then you can add the git method to your gemfile. You can also select a specific branch version. For example, here I have the gem cancan being pulled from the github repository on the 2.0 branch.
gem "cancan", :git => "git://github.com/ryanb/cancan.git", :branch => "2.0"
I've searched on Google, and I just found the uses of gem. As in, gem install, etc.
Are gems collections of .rb scripts?
If I build a series of scripts, for example that wraps the functionality of Google translate, is the preferred way of distributing that for usage a gem?
If not, how would I distribute this code?
According to RubyGems Wiki - RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a "gem"), a tool designed to easily manage the installation of gems, and a server for distributing them.
The gem command is used to build, upload, download, and install Gem packages.
Gem Usage
RubyGems is very similar to apt-get, portage, and yum in functionality.
Installation:
gem install mygem
Uninstallation:
gem uninstall mygem
Listing installed gems:
gem list --local
Gem Package Building
The gem command may also be used to build and maintain .gemspec and .gem files.
Build .gem from a .gemspec file:
gem build mygem.gemspec
For more info, refer to RubyGems Manuals.
Here are some nice tutorials :)
http://railscasts.com/episodes/135-making-a-gem
http://railscasts.com/episodes/245-new-gem-with-bundler
A gem is a module/Library that you can install and use in every project on your server.
A plugin is a module/Library that you can use inside your project
Indeed, if you make some code what you like to share you can make a gem or plugin of it. You can publish it on for example github.com. You can check the source of the existing gems on github if you like to know how to make a gem as well.
Gem Package Building
Step : gem build your_gem_name.gemspec
simple steps follow click here