Methods for detecting gem installation in Ruby _ruby topics

Source: Internet
Author: User
Tags try catch

Recently participated in the seven-cow demo contest, decided to use Ruby development. So I ran into some questions and then solved them, and here's a record.

In Ruby, Gem is a very common thing, it is the equivalent of Plug-ins, Ruby has a lot of great gems, to avoid the repetition of our wheels, my demo need to install gem, but in order to achieve better, first detect whether the gem has been installed, if not installed, continue to install, otherwise not installed.

So, how to detect gem in Ruby is installed, in fact, is also very simple, directly on the code can be. There is no need to explain too much. Begin...rescue ... Equivalent to a try catch in Java.

Copy Code code as follows:

#!/usr/bin/env Ruby
# Encoding:utf-8

def checkgemavailable (Gemname, Versionlimit=nil)
IsAvailable = False
Begin
if Versionlimit = = Nil
Gem Gemname
Else
Gem Gemname, Versionlimit
End
IsAvailable = True
Rescue Loaderror
End
IsAvailable
End

Run up and have a look

Copy Code code as follows:

Puts checkgemavailable (' rack ')
Puts checkgemavailable (' rack ', ' >=2 ')

The rack information of my machine

Copy Code code as follows:

Rack (1.5.2)

So the result of the above execution is

Copy Code code as follows:

True
False

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.