//
you're reading...
ChefCodeNotes

Ruby Class method to test for truth

I needed to test for truth in a Ruby class in my Chef LWRP cookbook and I wanted to use the question mark (?).

Simple way to do it in a resource library:

...
      def jdk_reinstall(arg=nil)
        set_or_return(:jdk_reinstall, arg, :kind_of => [TrueClass, FalseClass], :default => false)
      end

      def jdk_reinstall?
        return self.jdk_reinstall.class.eql?(TrueClass)
      end
....

Now in the provider I can do:


if @new_resource.jdk_reinstall?

This is just a snippet reminder, if someone wants more context on this feel free to post a comment.

Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s