- From: dreamcat four <
>
- To:
- Cc:
- Subject: [chef] Re: Re: Re: Re: Re: Re: Re: Re: 0.7.14 -> 0.7.16... issues
- Date: Wed, 10 Feb 2010 10:46:33 +0000
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; b=w42uKdqDe6c7sHtq/YZe6juWer38w3Ly2LwAqdLXA/vu+E83/CJPiKeTS/ZgH8ucVv ky6cpX7Trg+H0i+hFIwAioHRkRU+c4AEnyeiqrjidpVY4yo5d6/DqnFqquZ4nv8kKYxy UjgosmgyfRfI79YwmuLVcUuOL73JIVU0LrA9c=
Hi,
I think what you have to appreciate is that its a good programming
practise to replace questionable characters with an underscore in
identifiers and variable names. Regardless whether its a macro, a
constant, a symbol or an instance variable. Not only is it a
convention in other programming languages, its heavily the convention
in *ruby*. To try to use a dash instead offers few benefit, is not
needed for the functional execution of code, and creates more barrier
for other programmers who might want to come along and improve the
code.
In Bill's case its necessary for him to use an underscore instead of a
dash ('-') for the identifier symbol :snmp_master. The symbol's string
is converted into an instance variable. It may fail when encountering
a dash during the conversion later on.
Imagine this situation:
$ @snmp-master = true
NoMethodError: undefined method `-' for nil:NilClass
from (irb):12
from :0
wheras an underscore can slip through un-noticed
$ @snmp_master = true
=> true
However, that said,
When you wrap something in quotes, then its a string.
So its not a symbol. We were discussing symbols, right?
$ ruby -e "puts ':snmp-master'.class"
String
So your example isn't right if you were hoping to agree with Alex.
Although yes it is possible to prefix any string with colon : char.
Then ruby will turn the enclosed string into a symbol.
$ 'snmp-master'.to_sym
=> :"snmp-master"
$ 'snmp-master'.to_sym.class
=> Symbol
$ 'snmp-master'.to_sym.to_s
=> "snmp-master"
$ :snmp_master.to_s
=> "snmp_master"
Its bad idea because it can lead to a weird bug in other places.
In Ruby we are often converting between symbols and variables.
The assumption is everywhere that only underscore is allowed.
So Mathieu and Alex,
Please follow this programming convention if you want fewer bugs
On Wed, Feb 10, 2010 at 2:50 AM, msf
<
>
wrote:
>
On Tue, Feb 09, 2010 at 01:32:05PM -0800, Alex Soto wrote:
>
> I believe a dash is a valid character for an identifier/symbol in ruby
>
>
it is but you need to wrap it in quotes.
>
>
>> h = {}
>
=> {}
>
>> h[:snmp-master] = "foo"
>
NoMethodError: undefined method `-' for :snmp:Symbol
>
from (irb):4
>
>> h[':snmp-master'] = "foo"
>
=> "foo"
>
>
--
>
Mathieu Sauve-Frankel
>
Archive powered by MHonArc 2.6.16.