Do you see the difference between the following?:
puts 'Hello World!'
Hello World!
and
puts "Hello World!"
Hello World!
Strings can be defined either with single(') or double(") qoutes. In the example above this is maybe not reasonable, but there are more things where is can be reasonable.
String Interpolation is also only usable with double qoutes
name = 'Darth Vader'
puts "Hello #{name}"
Hello Darth Vader
if you do the same thing with single quotes it will look like this:
name = 'Darth Vader'
puts 'Hello #{name}'
Hello #{name}
One control character for example is the \n
, which is creating a line break.
Double quoted strings are interpolated, so things like these are only possible with double quotes.
puts "Hello \nDarth Vader"
Hello
Darth Vader
if you do the same thing with single quotes it will look like this:
puts 'Hello \nDarth Vader'
Hello \nDarth Vader
Symbols looks a little bit like Strings: :hello
,
but there are some differences, for example can a symbol not contain any spaces or non alpanumeric characters. So this is not possible:
:hello world
SyntaxError ((irb):25: syntax error, unexpected tIDENTIFIER, expecting end-of-input)
:hello world
^~~~~
Underscores(_) are commonly used for symbols, as replacement for whitespaces. The keyword here is snake_case So it will look like this, for example:
:hello_world
When the text is code then we use symbols, when the text is data then we use a string.
shopping_list = { bread: 'half' }
These makes hashes easier to read and write, because you don't need to use fancy hash rockets.
Kannst du den Unterschied zwischen den folgenden beiden Codeschnipseln erkennen?
puts 'Hello World!'
Hello World!
und
puts "Hello World!"
Hello World!
Strings können sowohl mit einfachen (') als auch doppelten (") Anführungszeichen definiert werden. Im obigen Beispiel macht es keinen Unterschied, aber es Fälle in denen es es Unterschiede macht.
Die sogenannten String Interpolation kann nur mit doppelten Anführungszeichen genutzt werden.
name = 'Darth Vader'
puts "Hello #{name}"
Hello Darth Vader
beim Versuch denselben Code mit einfachen Anführungszeichen zu schreiben sieht das Ergebnis wie folgt aus:
name = 'Darth Vader'
puts 'Hello #{name}'
Hello #{name}
Eines der bekannteren Steuerzeichen ist \n
, es erstellt einen Zeilenumbruch,
Da Strings aus doppelten Anführungszeichen interpoliert sind kann man folgendes
auch wieder nur mit selbigen machen:
puts "Hello \nDarth Vader"
Hello
Darth Vader
wenn du das gleiche mit einfachen Anführungszeichen machst, sieht die Ausgabe so aus:
puts 'Hello \nDarth Vader'
Hello \nDarth Vader
Symbols sehen ein bisschen wie Strings aus: :hello
, aber es gibt einige Unter-
schiede, zum Beispiel kann ein Symbol keine Leerzeichen oder nicht alphanumer-
ische Zeichen beinhalten. Folgendes geht demnach mit Symbols nicht:
:hello world
SyntaxError ((irb):25: syntax error, unexpected tIDENTIFIER, expecting end-of-input)
:hello world
^~~~~
Unterstriche werden üblicherweise bei Symbols als Ersatz für Leerzeichen verwendet. Das Schlüsselwort hierbei ist snake_case. Es sieht also zum Beispiel dann so aus:
:hello_world
Als gängige Praxis wird Text der als Code gilt üblicherweise in Symbols abgebildet und Daten wiederum als Strings.
shopping_list = { bread: 'half' }
Das macht Hashes angenehmer zu lesen und schreiben da mit Symbols als Keys keine Hash Rockets benötigt werden.