What is the use of Gemfile in rails?

前端 未结 5 989
萌比男神i
萌比男神i 2020-11-29 21:31

What is the use of Gemfile in rails?

How to use Gemfile?

相关标签:
5条回答
  • 2020-11-29 21:57

    During your development in Rails, there will be times where you will want to provide some functionality which is required by you, but either you don't know how to do or you don't want to implement it on your own since a lot of work has been put into its development by talented developers.

    These developments which you might need (user authentication, message system, asset handlers, geolocation, pagination system, linking to exterior services such as Amazon AWS, and last but not least Rails itself) are called Ruby Gems. These are ruby software packages, not necessarily relating to Rails, but since Rails is based on Ruby, 98% of the gems can be made availble to your Rails webapp code.

    Lots of gems can be found in github, but its funner to search for gems via ruby-gems or ruby-toolbox

    Your gemfile is a list of all gems that you want to include in the project. It is used with bundler (also a gem) to install, update, remove and otherwise manage your used gems.

    The gemfile has another purpose - you can group gems in :development, :test, :assets, :production, etc groups and Rails will know when to include the gems. For example:

    group :development, :test do
        gem "rspec-rails"
        gem "factory_girl_rails"
        gem "guard-rspec"
    end
    

    Note that on Rails 4, the assets group has been deprecated

    These gems belong to development environment and the test environment since they are for testing the application. You don't need them available in the production environment (you could, but that will bloat the memory unnecessarily).

    So - To use the gemfile, simply write the gem you wish to install such as

    gem 'devise'
    

    make sure to install bundler beforehand (in your console/cmd/ssh) with

    $ gem install bundler
    

    and then write in the console

    bundle install
    

    you will notice another gemfile appears! Gemfile.lock This file, as you will see if you open it with a text reader, lists all your gems with their version and their dependencies. This will come useful when you need to know which versions of the gems you installed.

    For more reading on the Gemfile - read on the bundler page

    for information regarding picking a gem you could start with this

    Good luck and have fun!


    Ok, so whats this Gemfile.lock that got created?

    Gemfile.lock, as the name suggests is a locking on all the versions of all the gems that got installed. So if Gemfile is what required to be installed, the lock file is what got installed and what version are actually required to get the app up and running.

    If you don't have the gems in that specific version (as specified in Gemfile.lock) rails will complain and you will have to either install the missing gems (via bundle install) or fix any conflicts manually (I believe bundler will give you some clues on that)

    Some things to know about Gemfile.lock

    • if you accidently delete it, it will get regenerated when you run bundle install. If you accidently delete Gemfile, you are out of luck.. You should use git :)
    • Heroku doesn't care about Gemfile.lock since it will reinstall all gems. So for Heroku, you must set the gem version you want, or Heroku will always install the latest version of gem, which may cause issues
    • Keep the Gemfile.lock in your project so you will always know what version of gems make your app work properly.
    0 讨论(0)
  • 2020-11-29 21:57

    Your system can have lots of gems ... thus can have multiple versions of same gem.

    A Gemfile specifies the list of gems with their versions that shall be used/loaded/(install if not present) whenever you run your rails application. or anything with bundle exec . .

    0 讨论(0)
  • 2020-11-29 22:02

    I'll try and give a simple answer

    Explanation by analogy

    Let's suppose you want to make a car. From scratch. Now that will be a nightmare. You need to build: the chasis, the engine, the corroborator, radiator, cooling, ventilation, etc etc. etc.

    Gems allow you to utilise car parts which other people have made before

    But you're not the only one building a car. Everyone's who's ever built a car has basically made the same things. There have been smart people who've gone out there and made combustion engines etc well before you were...........actually never mind.

    You needn't reinvent the wheel. Why make your own when you can simply get the best equipment straight off the shelf? What if you could get one of the best engines around, created by the most talented engineers in the world, without lifting a finger? Are you gonna spend a year trying to make your own?

    So basically rather than make everything yourself, you write down a shopping list of all the parts you need. And that is basically a gem file:

    • Rolls Royce Engine
    • AutoLive seatbelts
    • Michellin tyres.
    • PIAA Night headlights
    • etc etc.

    That my friend, is basically your gem file!

    0 讨论(0)
  • 2020-11-29 22:07

    Gemfiles are configuration for Bundler, which is used to manage your application's Ruby dependencies. That website includes a lot of documentation, including the Gemfile manual page.

    0 讨论(0)
  • 2020-11-29 22:21

    Firstly, what is a gem?

    According to Wikipedia:

    RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries

    Gemfile

    A Gemfile is a file we create which is used for describing gem dependencies for Ruby programs

    Now, in very very simple words:

    Gem can be thought of as a library which you can use in your code. Example: faker gem

    Your code can use the functionality of faker gem to produce fake data.

    Now you can list all the gems that your project requires in the gemfile. When you do a bundle install, all the gems in your gemfile are installed for you.

    0 讨论(0)
提交回复
热议问题