Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress particular warning in Ruby

I've seen plenty of posts providing the -W0 flag as an answer to this issue, but I don't want to suppress all warnings, just warnings of a particular value.

I'm running a non-rails app (which uses ActiveRecord, notwithstanding) on Ruby 1.8.7. I want to keep all warnings except for the following DEPRECATION WARNING:

Object#id will be deprecated; use Object#object_id

If that's not possible, I'd like to jettison all deprecation warnings. Java, at least, lets you do this. How about Ruby?

Update: I've upvoted both answers but checked the one that later searchers will expect to find here.

like image 222
JellicleCat Avatar asked Jul 31 '11 17:07

JellicleCat


2 Answers

If there's a specific section of code that produces the warnings, you could try mixing in the Kernel module from ActiveSupport and wrap it with a silence_warnings block (example pulled straight from the RDoc):

silence_warnings do
  value = do_something_that_causes_warning # no warning voiced
end

noisy_call # warning voiced

Is it absolutely necessary to suppress it? It's not like you're compiling something and have to sift through a ton of warnings all at once...

like image 99
Chris Mowforth Avatar answered Sep 25 '22 16:09

Chris Mowforth


Edit: If you use read_attribute(:id), then you should avoid the waring. Thanks Jeremy!

I'm not a Rails developer, but isn't there a method that allows you to say "I want the database field id, not the id method of the object"?

like image 31
Andrew Grimm Avatar answered Sep 23 '22 16:09

Andrew Grimm