You are here: PSPad forum > English discussion forum > Odd interpretation of 'symbol' in the context of Ruby symbol highlighting
Posted by: rovf | Date: 2020-09-08 10:35 | IP: IP Logged
We can set a distinctive colour in Settings/HighlighterSettings for the category "Symbol". At least in Ruby, symbols don't seem to be interpreted correctly.
For instance, if I have an assignment
var = :mysym
only the colon is highlighted in the colour for symbols, while `mysym` is highlighted like an identifier.
In the assignment
var = %s( mysym1 mysym2 )
nothing is highlighted for symbols. I would have expected, that all of :mysym, mysym1 and mysym2 would be painted in the symbol-colour.
--
Ronald Fischer (Germany)
Posted by: pspad | Date: 2020-09-08 10:48 | IP: IP Logged
Ruby uses general highlighter.
This highlighter doesn't construction var = :mysym
And in the line var = %s( mysym1 mysym2 ) highlighter doesn't know that mysym1 or mysim2 is something special.
There is on the flight highlight. PSpad doesn't contain any code compiler for all languages to keep in memory structures, definitions e.t.c.
I don't know Ruby (same as I don't know and don't use tens of other programming languages), but I am able to make some changes in general (user) highlighter, in case it won't have influence of other languages what are used by the general highlighter.
In this case I need exact description.
Posted by: rovf | Date: 2020-09-08 11:33 | IP: IP Logged
A symbol in Ruby is usually a colon, followed by an identifier::abc
or a colon followed by an operator::-
. If you could catch at least these cases, it would be helpful already.
It can also be a colon, immediately followed by a string::"a b"
. You need this if your symbol contains spaces. This is a symbol where the printed representation has an 'a', followed by two spaces, and then an 'b'.
The string can have single or double quotes::'a b'
You can also use%s(a b)
instead. As with all the %-constructs in Ruby, you can choose the delimiters.%s[a b]
or%sqa bq
would denote the same symbol. In the last example, the delimiter is the letterq
. I can choose it as a delimiter, because the symbol itself does not contain a q.
The idea with these delimiters is similar to regular expressions%r(....)
, and you find the same concept in the syntax of, say, Perl, so my guess you can steal some highlighting code from the way the Perl highlighter works.
--
Ronald Fischer (Germany)
Edited 1 time(s). Last edit at 2020-09-08 11:34 by rovf.
Posted by: pspad | Date: 2020-09-08 12:40 | IP: IP Logged
Perl has it's own highlighter, specialized for Perl only.
Rubby uses general (common) highlighter what is used for tens of user languages. I can't break functionality by adding new feature.
I see the symbols in Ruby are quite complicated. It similar as in all other interpreted languages what are expanded of new functionality what wasn't planned in the begin.
I will try what I am able to do.
Posted by: pspad | Date: 2020-09-08 18:05 | IP: IP Logged
I will add new option into user highlighter - Ruby symbols.
We can add functionality for the full support continuously.
Symbols will be highlighter with Label color.
Just now it works for :something anywhere in code.
Can you explain me case when colon is followed by operator? I tried to study Ruby symbols, but I didn't find this case.
Edited 1 time(s). Last edit at 2020-09-08 18:06 by pspad.
Posted by: pspad | Date: 2020-09-08 18:25 | IP: IP Logged
Just now PSPad knows already with new option:
ctrlv.cz
I think it's a good start
Posted by: rovf | Date: 2020-09-09 08:58 | IP: IP Logged
The string for a symbol can be any sequence of characters. Thats why the basic representation for a symbol is%s(...)
or%'abc'
The colon-identifier-notation (:abc
) is just a shortcut to cover the most common case. For the same reason, the colon-operator is an allowed shortcut, because this is also used often.
Here is a realistic example of using a symbol which has an operator:
Say I have an array of numbers and want to sum up the numbers, I can use the standard function reduce like this
sum = [2,5,4].reduce(0,&:+) # sets sum to 11
The 0 is obviously the initial value of the counter. The second parameter of reduce must be a object of type Proc (basically an anonymous method), which expects one argument and applies itself to the receiver; in the case of summing up, this Proc must add the next element to the running total. For generating this Proc object, we start with a the symbol:+
and apply to it the operator '&'. This operator implicitly invokes the methodto_proc
on the symbol. This method is defined in theSymbol
class and returns a suitable Proc object.
While having symbols representing operators is not uncommon (I think you find it in Lisp, Scheme, Smalltalk,...), I believe that the trick with the ampersand is fairly unique to Ruby.
--
Ronald Fischer (Germany)
Posted by: rovf | Date: 2020-09-09 12:16 | IP: IP Logged
pspad:Just now it works for :something anywhere in code.
The "anywhere" sounds good, because I forgot to mention one case:
Inside a parameter list of a function call, and inside a Hash denotation, an identifier followed by a colon denotes also a symbol. For instance, in both
func(abc: 13)
h = { abc: 13 }
we have the symbol%s(abc)
. The only point we have to be careful (as far as I can see) is the colon as part of a ternary "if"-operator. In
x=u?v:abc
abc is either a variable or a parameterless method invocation. No symbol in this example.
--
Ronald Fischer (Germany)
Editor PSPad - freeware editor, © 2001 - 2023 Jan Fiala, Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák, Privacy policy and GDPR