Ruby : How to write a gem? [closed]

Rubygems.org’s Guides is one of the best resources for writing your own gem. If you’re using Bundler in your app, you might want to look at Ryan Bigg’s guide to Developing a RubyGem using Bundler and the Railscast on creating gems with Bundler. If you’re interested in tools to help you write gems: Jeweler – … Read more

Should I use alias or alias_method?

alias_method can be redefined if need be. (it’s defined in the Module class.) alias‘s behavior changes depending on its scope and can be quite unpredictable at times. Verdict: Use alias_method – it gives you a ton more flexibility. Usage: def foo “foo” end alias_method :baz, :foo

Why does adding “sleep 1” in an after hook cause this Rspec/Capybara test to pass?

I suspect that it’s possible in your application for the comment to disappear from the page (which is the last thing you’re asserting) before it’s deleted from the database. That means that the test can clean up before the deletion happens. If this is the case you can fix it by waiting for the actual … Read more

Rails accepts_nested_attributes_for with f.fields_for and AJAX

It Is Possible There’s a very, very good tutorial on this here: http://pikender.in/2013/04/20/child-forms-using-fields_for-through-ajax-rails-way/ We also recently implemented this type of form on one of our development apps. If you goto & http://emailsystem.herokuapp.com, sign up (free) and click “New Message”. The “Subscribers” part uses this technology BTW we did this manually. Cocoon actually looks really good, … Read more

What is the behavior when using both positional and keyword arguments in Ruby?

The order is as follows: required arguments arguments with default values (arg=default_value notation) optional arguments (*args notation, sometimes called “splat parameter”) required arguments, again keyword arguments optional (arg:default_value notation, since 2.0.0) intermixed with required (arg: notation, since 2.1.0) arbitrary keyword arguments (**args notation, since 2.0.0) block argument (&blk notation) For example: def test(a, b=0, *c, … Read more

Determine file type in Ruby

There is a ruby binding to libmagic that does what you need. It is available as a gem named ruby-filemagic: gem install ruby-filemagic Require libmagic-dev. The documentation seems a little thin, but this should get you started: $ irb irb(main):001:0> require ‘filemagic’ => true irb(main):002:0> fm = FileMagic.new => #<FileMagic:0x7fd4afb0> irb(main):003:0> fm.file(‘foo.zip’) => “Zip archive … Read more