Notes posted by artemave
RSS feed
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

2 thanks

0 thanks

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.

0 thanks
Pluralize Without Count (inline version)
= pluralize(item.categories.count, ‘Category’).sub(/d+s/, ”)

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