On Tue, Jul 24, 2012 at 10:37:47PM +0200, Brice Figureau wrote:
> On 24/07/12 22:30, Matthew Probst wrote:
> > Actually, I had it _that_ way before I tried the ways I posted
> > previously, in imitation of the exact same code.  But, to make sure I
> > wasn't just messing it up, I went ahead and tried it again with this code:
> > 
> > # subpaths.rb
> > #
> > module Puppet::Parser::Functions 
> >   newfunction(:subpaths, :type => :rvalue) do |args|

all arguments that you pass to your custom function are now represented
by args. So if you call your function like

    subpaths('foo', 'bar')

args will ['foo', 'bar']. If you call your function like

    subpaths(['foo', 'bar'])

then you really just pass one argument to your function, so args[0] is
['foo', 'bar'] and args = [['foo', 'bar']]

> >     args = [args] unless args.is_a?(Array)
> >     args.each do |path|

if you called your function like subpaths(['foo', 'bar']) then your
first temp_path will be the array ['foo', 'bar']

> >       temp_path = path
> >       begin
> >         result << temp_path
> >         temp_path = File.dirname(temp_path) 

File.dirname fails if you pass an array with "can't convert Array into
String"

> >       end while (temp_path != "/" and temp_path != ".")
> >     end
> >     return result.uniq

result is only valid inside your args.each block.

> >   end
> > end
> > 

I hope this helps understanding why this failed. In your last (working
example) you first extracted the first argument with arguments[0] and you
define result outside the each block.

-Stefan

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to