Flowdock

Notes posted by artemave

RSS feed
December 29, 2011
0 thanks

Use kill 0 to find out if process is running

is_running.rb:

#!/usr/bin/env ruby

pid = ARGV[0].to_i

begin
  Process.kill(0, pid)
  puts "#{pid} is running"
rescue Errno::EPERM                     # changed uid
  puts "No permission to query #{pid}!";
rescue Errno::ESRCH
  puts "#{pid} is NOT running.";      # or zombied
rescue
  puts "Unable to determine status for #{pid} : #{$!}"
end

Thanks to http://stackoverflow.com/a/200568/51209

August 11, 2011
2 thanks

More AREL compliant style is also possible

Code example

Person.where(:name => 'David').exists?
August 4, 2011
0 thanks

Tempfile extension

Code

f = Tempfile.new(['graph','.json'])
November 9, 2010
10 thanks

NOT Equivalent to Array#reject!

@tadman is wrong. There is a difference and, trust me, it can bite:

1.9.2 > [1,2,3,4].delete_if {|x| x > 10}
 => [1, 2, 3, 4] 
1.9.2 > [1,2,3,4].reject! {|x| x > 10}
 => nil 

That is, if reject! hasn’t rejected anything, it returns nil.

October 27, 2010
0 thanks

Pluralize Without Count (inline version)

= pluralize(item.categories.count, ‘Category’).sub(/d+s/, ”)

May 6, 2010
1 thank

has_and_belongs_to_many_with_deferred_save

Be aware that has_and_belongs_to_many saves association to join table immediately after assign. It does NOT wait for my_object.save. Hence if save does not get through validations (or fail for any other reason), associated records will still be in the database.

Here is a nice workaround: http://github.com/TylerRick/has_and_belongs_to_many_with_deferred_save