Now, we will learn how to utilize RubyMine to manage your Gems and external libraries used for your Ruby on Rails programs.
Start RubyMine and open the HelloWorld
project that we created earlier. We will be adding an implant to enhance your assimilation process into the collective.
We will now use a Gem in RubyMine by showing the following simple example using the Prawn Gem:
Right-click on the
HelloWorld
project folder in the left project panel and add a new file to your project. Name itpdf.rb
.Add the following code to this new file:
require 'prawn' Prawn::Document.generate("hello.pdf") do |pdf| pdf.font "Courier" pdf.stroke_color = "ff0000" pdf.line_width = 5 pdf.stroke do pdf.circle([300, 300], 100); pdf.circle([350, 320], 20); pdf.circle([260, 325], 20); pdf.curve [270, 250], [340, 250], :bounds => [[270, 270],[275, 220]] end pdf.move_down(300) pdf.font_size = 36 pdf.fill_color "000000" pdf.draw_text("You will be assimilated!", :at => [40, 40]) pdf.line_width = 1 pdf.stroke_color = "0000ff" pdf.move_to([0, 0]) grid = 11 num_lines = 51 size = (num_lines - 1) * grid pdf.stroke do 51.times do |idx| pdf.line([0, idx*grid], [size, idx*grid]) pdf.line([idx*grid, 0], [idx*grid, size]) end end end
Now, right-click on the open source file and select
Run pdf
. You should get an error similar to in 'require': cannot load such file -- prawn (LoadError). This means that you do not have the required technology to continue the assimilation process. We need to add some Ruby Gems to allow our program to continue.Open the Settings panel and select the Ruby SDK and Gems panel from the left-hand list. This is where we also changed the version of Ruby that we are using. This panel will allow us to install some specific Ruby Gems that we need.
Hit the button Install Gems... and you will see a screen like the following:
Start typing the name of the Gem you wish to install, which in this case is
prawn
and the search will begin immediately. Scroll to the right Gem.Select the Gem in the list at the left-hand side and then hit the button Install. RubyMine will then run the Gem system and install the Gem and all of its appropriate dependencies into your system.
When it is complete, select the prawn gem in the list on the right and you will see the description panel filled with various aspects of the gem for your browsing pleasure.
Once completed, go back and re-run the
pdf.rb
program.Since this program actually generates a PDF file, we need to find where it saved the file. In the project window on the left, hit the icon that looks like the following:
This will synchronize the files inside the folder with the project list. You should now be able to see the file
hello.pdf
in the project window.Double-click on this and you will see this file in whichever application you have on your computer that displays PDF files. You should see something like the following:
The code that we typed in is a simple use of the Prawn Gem. It first requires the Gem code and then starts a block of code that will begin generating the hello.pdf
file on the disk. Each command then sets properties of the text, size, colors, circles, and finally draws a grid of lines on the page.