How to use ruby libraries in rails? - ruby-on-rails

I just installed a ruby gem
gem install anemone
But I cannot hit off to use it directly in my rails application, this is the first time I'm using an external ruby library so it will be very helpful if you can give me an insight into it rather than just a solution
Cheers

In your Gemfile add:
gem 'anemone'
This way Rails will load the library when it starts, and you can then use it. You don't even have to use the require keyword.

You must put used libraries into Gemfile. Everything about gemfiles is available on Bundler page.

There's an official Rails guide about this topic.

You also need to use 'require' and the name of the library on top of the file where you want to use the library. For example if you are using nokogiri you would need to add the line require 'nokogiri'

Related

Ruby on Rails: where to put the 'require' files?

I'm trying to use this gem barometer and in the document it says that it can be used right out of the box with using require 'barometer'
I've always used gems and put them in the gemfile, but I think this is different...
Do I just download this entire repo, and copy all the files in the lib folder into my vendor folder? or maybe public folder?
Where would you typically put these files? And where would you include the require? Should this be in the application controller? Or maybe in the helper? Sorry for this really noob question.
I know in my local environment, I can just type in gem install barometer in my console, and not have to put in a require, but I don't think this will work in heroku, or production environment.
I've always used gems and put them in the gemfile, but I think this is different...
No, this is no different. Barometer is a Rubygem and putting it in your Gemfile is exactly the way to use it.
As with every library, your require should go in whichever file uses the code, for example the same file that the Barometer.new call is. You don't always need the require line depending on your Ruby environment, but it's always a good idea to get used to it

Bootstrap gem in Rails

I'm going to create a new Rails project, and as I know I should use gem 'bootstrap-sass', it's not a problem for me. But I have one question - why I should use gem instead of just adding some files (CSS and JS) directly to index.html? Can you explain me? Thanks in advance!
Use gem or source files don't make any big difference.
But, when you use gem it's easier to update... you need just update the gem and it's done! And when you use the source files, you must download the new version and replace the old files. I think it's the big deal when using gem: easer to update.

what's the point of having a jquery-rails

So I installed Ruby on Rails(first time) and got down to starting a new project and a server. So as expected there was an error on running the Rails server command. Some gems needed to be installed, apparently. The bundle install command didn't work for some wierd ssl issue. So I decided to do it all
manually.
What strikes me as odd is that there's a gem for jQuery: jquery-rails. I don't understand the point of this, really. Why not simply download the jquery file and put it in public.
What's the point of having a gem here.
Is it simply convinience or is there another important reason behind it?
Jquery-rails does 2 things. First of all it bundles the appropriate version of jquery. I consider this only a convenience for jquery itself. For something like jquery-ui, jquery-ui-rails does a lot more: since jquery-ui is modular, that gem will serve to clients only the bits of jquery-ui you are using.
The other thing in jquery-rails is jquery-ujs. This is javascript that makes things such as passing the :remote => true option to form_for work. It used to be that rails itself contained a version of this for prototype, but with rails 3 this was extracted from rails to make it easier to use other javascript libraries than rails' previous default of prototype
I think this is a good idea, because it makes JQuery available as a versioned dependency.
If you need to update JQuery, you just have to change the version at one place in your app, and every page that needs it will use the new version.
And based on its homepage, there's also a test helper that you can use in your tests.

Making my first gem - Where do I start?

So I have this idea for a RubyGem that I think would be an awesome experience to learn more about Ruby and Rails but...I have no idea where to start.
My idea is to generate a folder "articles" where you can put markdown files. From this folder the main blog page displays only the titles as links to the articles themselves.
It sounds simple but I honestly have no idea where to start. What articles do you recommend I read if I want to insert lines into routes.rb, generate a folder and display markdown in Rails?
I would recommend one of these tutorials for gem creation:
http://net.tutsplus.com/tutorials/ruby/gem-creation-with-bundler/
http://railscasts.com/episodes/245-new-gem-with-bundler
To modify the routes.rb file, you'll just need File.open to read lines in. Use regular expressions to determine where you want to insert your line, and write the file back out.
To create a folder, look at the documentation for Dir.new
For Markdown in Ruby/Rails, I like the rdiscount gem: https://github.com/rtomayko/rdiscount
Railties provide a nice way to do certain things like this. You'll probably use http://api.rubyonrails.org quite a bit. There is some Railtie documentation on that site here: http://api.rubyonrails.org/classes/Rails/Railtie.html.
I recommend reading the RubyGems guides – especially What is a gem?, Make your own gem and Patterns.
Since you're likely already using Bundler, you can run bundle gem <name> to generate a gem project with stuff already in place. It does save work, but refer to the guides if there's something you don't understand.
Also, watch some open source projects on GitHub – observing other developers and taking note of how they do things certainly helps.
The simplest way is probably to read other gems that do anything similar to what you want to accomplish. Start with their .gemspec files that will list all the other files which are needed for the gem to work, and a list of gem dependencies.
Responding more to how to get started with creating gems, the following are 2 popular, documented gems that can help you.
https://github.com/seattlerb/hoe
https://github.com/technicalpickles/jeweler
Also, though it does more than you're trying to do with your gem (it's a static site generator), https://github.com/mojombo/jekyll is a very popular gem which you place .markdown files into a posts/ directory which are converted to static HTML pages via rake. I would imagine you could find at least some functionality you're after there.

Creating my own PaperClip GEM? for Heroku?

based on the needs of my app, I need to make a modification to paperclip, it's only one line but it makes all the difference for my apps needs.
In my Rails 3 GEM FILE I have the following:
gem 'paperclip', '2.3.5'
With github, how does one go about making there own paperclip GEM? So I can use it on Heroku?
Something like
gem 'paperclip', '2.3.5' -> my version?
Would love to hear how this is done. How this is maintained as paperclip is updated. and how I use this locally and on Heroku.
Thanks
Here's the github page on 'Forking'. http://help.github.com/forking/
You must fork the repo on github. Then, instead of passing a version in the Gemfile, pass the following:
gem 'paperclip', :git=>'git://github.com/username/repo.git'
Why are you creating a gem for this? You can monkey patch the gem. This will allow you to use the standard version. What is the change you need? What is the link to your gem, may be there is an easier solution.

Resources