How to search for useful ruby gems
- Github is where most gems source code is hosted; they have a search facility, and you can search by language as well.
Ruby tool to browse through installed gems code
You could use pry with source-browsing.
It's a console similar to IRB, and you can view the definition of any class/method that is written in plain Ruby.
Here's an example with rgeo
gem :
> pry
[1] pry(main)> require 'rgeo'
=> true
[2] pry(main)> show-source RGeo::Cartesian.factory
From: ~/.rvm/gems/ruby-2.3.1/gems/rgeo-0.6.0/lib/rgeo/cartesian/interface.rb @ line 27:
Owner: #<Class:RGeo::Cartesian>
Visibility: public
Number of lines: 7
def preferred_factory(opts_ = {})
if ::RGeo::Geos.supported?
::RGeo::Geos.factory(opts_)
else
simple_factory(opts_)
end
end
Best way to search through the source code of all the gems for a project
I usually do the following:
cd `bundle show rails` # Go to the directory having rails gem content
cd .. # Go one level up to the folder having all gems
grep -ir escape_javascript * # search for the required text in all files
> actionview-4.1.6/lib/action_view/helpers/javascript_helper.rb: def escape_javascript(javascript)
> actionview-4.1.6/lib/action_view/helpers/javascript_helper.rb: alias_method :j, :escape_javascript
EDIT: The answer below by jrochkind is the correct answer; my answer is incorrect as it searches through all the gems installed in the system.
List of installed gems?
The Gem command is included with Ruby 1.9+ now, and is a standard addition to Ruby pre-1.9.
require 'rubygems'
name = /^/i
dep = Gem::Dependency.new(name, Gem::Requirement.default)
specs = Gem.source_index.search(dep)
puts specs[0..5].map{ |s| "#{s.name} #{s.version}" }
# >> Platform 0.4.0
# >> abstract 1.0.0
# >> actionmailer 3.0.5
# >> actionpack 3.0.5
# >> activemodel 3.0.5
# >> activerecord 3.0.5
Here's an updated way to get a list:
require 'rubygems'
def local_gems
Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
end
Because local_gems
relies on group_by
, it returns a hash of the gems, where the key is the gem's name, and the value is an array of the gem specifications. The value is an array of the instances of that gem that is installed, sorted by the version number.
That makes it possible to do things like:
my_local_gems = local_gems()
my_local_gems['actionmailer']
# => [Gem::Specification.new do |s|
# s.authors = ["David Heinemeier Hansson"]
# s.date = Time.utc(2013, 12, 3)
# s.dependencies = [Gem::Dependency.new("actionpack",
# Gem::Requirement.new(["= 4.0.2"]),
# :runtime),
# Gem::Dependency.new("mail",
# Gem::Requirement.new(["~> 2.5.4"]),
# :runtime)]
# s.description = "Email on Rails. Compose, deliver, receive, and test emails using the familiar controller/view pattern. First-class support for multipart email and attachments."
# s.email = "david@loudthinking.com"
# s.homepage = "http://www.rubyonrails.org"
# s.licenses = ["MIT"]
# s.name = "actionmailer"
# s.require_paths = ["lib"]
# s.required_ruby_version = Gem::Requirement.new([">= 1.9.3"])
# s.requirements = ["none"]
# s.rubygems_version = "2.0.14"
# s.specification_version = 4
# s.summary = "Email composition, delivery, and receiving framework (part of Rails)."
# s.version = Gem::Version.new("4.0.2")
# end]
And:
puts my_local_gems.map{ |name, specs|
[
name,
specs.map{ |spec| spec.version.to_s }.join(',')
].join(' ')
}
# >> actionmailer 4.0.2
...
# >> arel 4.0.1,5.0.0
...
# >> ZenTest 4.9.5
# >> zucker 13.1
The last example is similar to the gem query --local
command-line, only you have access to all the information for a particular gem's specification.
How to search for gems inside of Ruby code, or where did Gem::RemoteInstaller go?
Gem::RemoteInstaller
has been removed from the rubygems since version 1.0. This was a while ago. If you are looking for the ability to set someone up with gems that they may not have, I would strongly suggest bundler
(actually I think all ruby projects should use it) http://gembundler.com
OK, you can try:
require 'rubygems'
r = Gem::SpecFetcher.new
r.suggest_gems_from_name('rails')
=> ["rails"]
Determine Location Of Ruby Gems
you can try
gem which rails
to fetch location for particular gem, or
echo $GEM_HOME
to fetch home dir of your gems
How to find apps on Github, that use a specific Ruby gem?/contain a specific line of code?
Ok, now it can easily be done in github by looking at /network/dependencies
path like https://github.com/jekuno/milia/network/dependencies
Related Topics
Include Erb Delimiters Inside of a String in an Erb Block
Is There a More Concise Way to Call an Outside Method on a Map in Ruby
What Is the Advantage of Creating an Enumerable Object Using To_Enum in Ruby
Encoding Issues in JavaScript Files Using Rails Asset Pipeline
What Are Fast Xml Parsers for Ruby
Detect When a Devise Session Expires
Ruby Converting Utc to User's Time Zone
Cron Is Running in Home Directory Instead of File Directory
How to Simplify or Clean Up This Anagram Method
Flash Message with HTML_Safe from the Controller in Rails 4 (Safe Version)
Web Page Scraping Gems/Tools Available in Ruby