Is it possible to run a custom function on irb (Interactive Ruby console)?

441 views
Skip to first unread message

Francois Lafont

unread,
Dec 19, 2014, 10:45:48 PM12/19/14
to puppet...@googlegroups.com
Hi,

In a personal module of my puppetmaster (in the production environment),
I have a custom function which I would like to run on irb (to make
quick test, it would be so convenient). Is it possible?

Here is my environment:
- OS => Debian Wheezy
- Puppet => 3.7.3
- Ruby => 1.9.3p194

I have read to do this we can do:

~$ irb
irb> require 'puppet'
irb> require '/puppet/production/modules/homemade_functions/lib/puppet/parser/functions/validate_non_empty_data.rb'
irb> Puppet::Parser::Functions.function(:validate_non_empty_data)

But I doesn't work for me:

----------------------------
# irb
irb(main):001:0> require 'puppet'
=> true
irb(main):002:0> require '/puppet/production/modules/homemade_functions/lib/puppet/parser/functions/validate_non_empty_data.rb'
NoMethodError: undefined method `[]' for nil:NilClass
from /usr/lib/ruby/vendor_ruby/puppet/parser/functions.rb:46:in `environment_module'
from /usr/lib/ruby/vendor_ruby/puppet/parser/functions.rb:259:in `get_function'
from /usr/lib/ruby/vendor_ruby/puppet/parser/functions.rb:139:in `newfunction'
from /puppet/production/modules/homemade_functions/lib/puppet/parser/functions/validate_non_empty_data.rb:2:in `<module:Functions>'
from /puppet/production/modules/homemade_functions/lib/puppet/parser/functions/validate_non_empty_data.rb:1:in `<top (required)>'
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
----------------------------

I probably miss instructions to recreate the proper execution environment
but which ones?

Thanks in advance for your help.

--
François Lafont

Felix Frank

unread,
Dec 23, 2014, 7:13:42 PM12/23/14
to puppet...@googlegroups.com
On 12/20/2014 04:45 AM, Francois Lafont wrote:
> Hi,
>
> In a personal module of my puppetmaster (in the production environment),
> I have a custom function which I would like to run on irb (to make
> quick test, it would be so convenient). Is it possible?

Hmm, I honestly don't know, but the idea sounds sort of horrible to me :-)

Can you not solve this the other way around? Put the code you need in a
Ruby class that is globally available on your system.

require 'my/data_validator'
My::DataValidator.validate(@some_value)

The class is defined in /some/ruby/lib/dir/my/data_validator.rb

You can then refactor your parser function to use a variation of just
that pseudo code above.

HTH,
Felix
Reply all
Reply to author
Forward
0 new messages