Question about ruby ​​symbols

Possible duplicates:
Why are projects no longer using Ruby characters instead of strings? What is the difference between a string and a character in Ruby?

I am new to ruby ​​language. From what I read, I should use characters instead of strings where possible. It is right?

+3
source share
4 answers

You do not have to do anything. However, it is recommended that you use characters instead of strings in cases where you will use the same string literal again and again. The reason is that only one character is stored in memory, whereas if you use a string literal, it will be created every time you assign it, therefore potentially losing memory.

+5

, . .

, , .

+4

, - , , . , .

, , . , , ; -, .

Ruby Newbie guide to symbols:

: . - , , (), .

, ( ), , .

, . - - ( , ) .

String . String . .

+1

Symbols have two main advantages:

  • They are similar to inline strings, so they can improve performance.
  • They are syntactically distinct, which can make your code more readable, especially for programs that use hash tables as complex embedded data structures.
0
source

Source: https://habr.com/ru/post/1779663/


All Articles