[Puppet Users] Re: how to pass the value to custom function?

2013-10-24 Thread jcbollinger


On Wednesday, October 23, 2013 9:43:25 AM UTC-5, Sans wrote:
>
> Thanks John!
> That fixed problem. I believe, in that, case symbol should be used over 
> string in a hash? Cheers!!
>
>

>From a functional point of view, either strings or symbols are usable as 
hash keys; you just need to be consistent.  Hash keys with the same hash 
value are compared with each other via their eql?() methods.  String's 
eql?() method returns true when its argument is another string having the 
same length and content, so you can get the results you expect and want 
with string keys.


John

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.


[Puppet Users] Re: how to pass the value to custom function?

2013-10-23 Thread Sans
Thanks John!
That fixed problem. I believe, in that, case symbol should be used over 
string in a hash? Cheers!!


On Tuesday, October 22, 2013 2:12:17 PM UTC+1, jcbollinger wrote:
>
>
>
> Your hash keys are symbols, whereas argument 0 to the function is a 
> string.  'linux' != :linux.  Try adding
>
>   cls = cls.to_sym if cls.is_a? String 
>
> between initially setting cls and testing whether it is a key of your 
> hash.
>
>
> John
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.


[Puppet Users] Re: how to pass the value to custom function?

2013-10-22 Thread jcbollinger


On Monday, October 21, 2013 9:59:19 AM UTC-5, Sans wrote:
>
> Dear all,
>
> I'm trying this thing for a while but can't figure out what am I doing 
> wrong. 
> Here is my sample function (which is similar to the original one, except 
> for the hash, which is generated dynamically in the original one):
>
> module Puppet::Parser::Functions
>> newfunction(:am_running_oss, :type => :rvalue ) do |args|
>>
>> oss = {:linux=>["Slackware", "RedHat", "Caldera"],
>>:mac=>["Jaguar", "Lion", "Tiger", "Kodiak"],
>>:win=>["Chicago", "Daytona", "Longhorn"]}
>> 
>> cls = args[0] 
>> 
>> if oss.key?(cls)
>> return oss[cls][0]
>> else
>> return 'undefined'
>> end
>> end
>> end
>>
>
> and then in my module's init.pp, I have this:
>
>
> $h= am_running_oss($::am_os_type)
>> notify { "=*=*= amRunningOS <|:|> ${h} =*=*=*=*=*=*=*=": }
>>
>
>
> (am_os_type is a fact, that returns *win*, *mac* or *linux* based on the 
> node type)
>
> I was expecting to see *Jaguar* as the return value but I get 
> *undefined*instead. What am I doing wrong? Is there anything still am I 
> missing in 
> terms of passing the *args* to the  function? Cheers!!
>


Your hash keys are symbols, whereas argument 0 to the function is a 
string.  'linux' != :linux.  Try adding

  cls = cls.to_sym if cls.is_a? String 

between initially setting cls and testing whether it is a key of your hash.


John

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.