ruby - Symbol literal or a method -
are :"foo"
, :'foo'
notations quotations symbol literal, or :
unary operator on string?
:
part of literal enter or create through method. although :
can take name or "string"
create literal, unlike operator not provoke action or modify value.
in each case instance of symbol returned. writing :
string notation important. if want represent, instance, string containg whitespace symbol need use string notation.
> :foo => :foo > :foo bar syntaxerror: (irb):2: syntax error, unexpected tidentifier, expecting end-of-input > :"foo bar" => :"foo bar"
furthermore, interesting explore equality operator (==)
> :"foo" == :foo => true > :"foo " == :foo => false
my advice, not think of passing string or name create symbol, of different ways express same symbol. in end enter interpreted object. can achieved in different ways.
> :"foo" => :foo
after all, %w(foo bar)
alternative way of writing ['foo', 'bar']
.
Comments
Post a Comment