Friday, March 20, 2020

Understanding String Literals in Ruby

Understanding String Literals in Ruby String objects hold ordered sequences of bytes, typically characters, usually to form pieces of human-readable text. Theyre a very common object type in all programming languages, and Ruby has a number of high-level and a few low-level ways to create, access and manipulate String objects. Strings are most often created with a String literal. A literal is a special syntax in the Ruby language that creates an object of a specific type. For example, 23 is a literal that creates a ​Fixnum object. As for String literals, there are several forms. Single-Quotes and Double-Quoted Strings Most languages have a String literal similar to this, so this may be familiar. The types of quotes, (single quote, apostrophe or hard quote) and (double quote or soft quote) are used to enclose string literals, anything between them will be turned into String objects. The following example demonstrates this. But there are some differences between single and double quotes. Double quotes  or soft quotes enable some magic to happen behind the scenes. Most useful is interpolation inside strings, useful for inserting the value of a variable into the middle of a string. This is achieved by using the #{ †¦ } sequence. The following example will ask you for your name and greet you, using interpolation to insert your name into the string literal thats printed. Note that any code can go inside the braces, not just variable names. Ruby will evaluate that code and whatever is returned it will attempt to insert it into the string. So you could just as easily say Hello, #{gets.chomp} and forget about the name variable. However, its good practice not to put long expressions inside the braces. Single quotes, apostrophes, or hard quotes are much more restrictive. Inside of the single quotes, Ruby will perform no interpolation or escape sequences other than escaping the single quote character and backslash itself (\ and \\ respectively). If you dont intend to use interpolation, its recommended to use single quotes more often than not. The following example will attempt to interpolate a variable inside of single quotes. If you run this youll get no error, but what will be printed? The interpolation sequence was passed through uninterpreted. When Should I Use Single and Double Quotes This is a matter of style. Some prefer to use double quotes all of the time unless they become inconvenient. Others would rather use single quotes unless the interpolation behavior is intended. Theres nothing inherently dangerous about using double quotes all of the time, but it does make some code easier to read. You dont need to read a string when reading through code if you know there are no interpolations in it  because you know the string itself wont have any side effects. So which string literal form you use is up to you, there is no real right and wrong way here. Escape Sequences What if, in a string literal, you want to include a quote character? For instance, the string Steve said Moo!  wont work. And neither will Cant touch this!. Both of these strings include the quote character inside of the string, effectively ending the string literal and causing a syntax error. You could switch quote characters, like Steve said Moo!, but that doesnt really solve the problem. Instead, you can escape any quote character inside the string, and it will lose its special meaning (in this case, the special meaning is to close the string). To escape a character, prepend it with the backslash character. The backslash character tells Ruby to ignore any special meaning the next character may have. If its a matching quote character, dont end the string. If its a hash sign, dont start an interpolation block. The following example demonstrates this use of backslash to escape special characters. The backslash character can be used to remove any special meaning from the following character but, confusingly, it can also be used to denote special behavior in double-quoted strings. Most of these special behaviors have to do with inserting characters and byte sequences that cannot be typed or represented visually. Not all Strings are character strings  or may contain control sequences intended for the terminal, and not the user. Ruby gives you the ability to insert these types of strings using the backslash escape character. \n - A newline character. The puts method does this automatically, but if you wish to insert one in the middle of a string, or the string is destined for something other than the puts method, you can use this to insert a newline in a string.\t - A tab character. The tab character moves the cursor over (on most terminals) to a multiple of 8, so this is very useful for display tabular data. However, there are better ways of doing this, and using the tab character is considered a bit archaic or hackish.\nnn - A backslash followed by 3 numbers will denote an ASCII character represented by 3 octal digits. Why octal? Mostly for historical reasons.\xnn - A backslash, an x, and 2 hex digits. The same as the octal version, only with hex digits. Youll probably never use most of these, but know that they exist. And also remember that they only work in double-quoted strings. The next page discusses multi-line strings and an alternate syntax for string literals. Multi-Line Strings Most languages dont allow multi-line string literals, but Ruby does. Theres no need to end your strings and append more strings for the next line, Ruby handles multi-line string literals just fine with the default  syntax. Alternate Syntax As with most other literals, Ruby provides an alternate syntax for string literals. If youre using a lot of quote characters inside your literals, for example, you may want to use this syntax. When you use this syntax is a matter of style, theyre usually not needed for strings. To use the alternate syntax, use the following sequence for single-quoted strings  %q{ †¦ }. Similarly, use the following syntax for double-quoted strings  %Q{ †¦ }. This alternate syntax follows all the same rules as their normal cousins.  Also,  note that you can use any characters you wish instead of braces. If you use a brace, square bracket, angle bracket or parenthesis, then the matching character will end the literal. If you dont want to use matching characters, you can use any other symbol (anything not a letter or number). The literal will be closed with another of the same symbol. The following example shows you several ways to use this syntax. The alternate syntax also works as a multi-line string.

Wednesday, March 4, 2020

The Many Meanings of the Spanish Ya

The Many Meanings of the Spanish Ya Already, right now, still, enough - these are just four of the dozens of possible translations of the Spanish ya. Ya, which is usually an adverb but sometimes a conjunction, is one of those words whose meaning depends almost entirely on the context. Sometimes it doesnt have much of a translatable meaning, becoming a filler word something like pues, adding a slight amount of emotional content to a sentence (although the exact nature of the emotional content may be difficult to determine out of the context). Key Takeaways Ya  is usually an adverb, very common in speech, whose meaning depends almost on the context. It can express both resignation and surprise, both agreement and disbelief.The most common translations of  ya  include now, still, and already.Sometimes,  ya  doesnt need to be translated, as it can function as a filler word or a word that adds a vague emotional content rather than a denotation. Most Common Meanings: 'Now' and 'Already' The most common meanings of ya are now and already. Often, it signifies a slight amount of impatience, although it sometimes can indicate satisfaction or agreement with the person being spoken to. As you might have guessed, its a word youll come across more often in informal conversation than you will in formal writing. When the verb of the sentence is in a past tense, already is usually a good translation: Lo he leà ­do ya. (Ive already read it.)El lunes ya lo habrà © visto. (By Monday I will have already seen it.) ¿Ya compraste tu boleto para la loterà ­a? (Did you already buy your ticket for the lottery?)No se puede romper lo ya que est roto. (You cant break whats already broken.) When the verb refers to an anticipated action, now is a common meaning. If the context or tone of voice suggests impatience, right now can also be used: Ya est aquà ­. (Shes here now.)Ya salen. (Theyre leaving now.)Lo quiero ya. (I want it right now.)Tienes que estudiar ya. (You need to study right now.) In some situations, you may be able to use either already or now in translation, such as when expressing surprise. The first sentence above, for example, might have been translated as Shes already here. And the question  ¿Sales ya? could mean either Youre leaving now? or Youre leaving already? When being rude,  ¡Corta ya! could be translated as either Shut up now! or Shut up already! Other Translations for Ya There are dozens of other ways you could interpret ya. Here are examples of other ways you could interpret ya: Still, any more (especially when used in the negative): Ya no trabaja aquà ­. (He doesnt work here any more.) Ya no estn ganando dinero en la situacià ³n actual. (They arent making money any more in the current situation.)To note that a wish has been satisfied:  ¡Ya conseguà ­ el trabajo! (I got the job!) Ya entiendo las diferencias. (Finally I understand the differences.)To indicate frustration:  ¡Basta ya! (Enough is enough!)  ¡Ya est bien! (Thats plenty!)  ¡Ya era hora! (Its about time!)  ¡Vete ya! (Get yourself out of here!)To indicate emphasis:  ¡Ya lo sà ©! (I already know that!) Es difà ­cil, ya vers. (Its difficult, youll see.) Ya puedes empezar a estudiar. (You had better start studying.) Él no comià ³, que ya es decir. (He didnt eat, which is saying something.) Ya me gustarà ­a ser inteligente. (Id love to be intelligent.)Later (to indicate something will happen in the indefinite future): Ya ocurrir. (Itll happen.) Ya lo harà ©. (Ill get it done.) Ex celente. Ya hablaremos. (Excellent. Well talk later.) To express agreement or incredulity:  ¡Ya, ya! (Oh, sure!) Ya, y el papa es luterano. (Sure, and the pope is Lutheran.) Ya, pero es difà ­cil. (Yes, but its difficult.)To call attention to something, especially when followed by que: Ya que no est aquà ­, podemos salir. (Considering that hes not here, we can leave.) Ya que conocemos es fcil, podemos hacerlo. (Since we know its easy, we can do it.)To offer reassurance: Ya aprobars el examen. (Youll pass the test.) Ya sabrs pronto. (Youll know soon.)To emphasize a connection between different facts: Yo quisiera consultarte sobre este tema, ya que mi perro tiene esta conducta en diferentes situaciones. (I would like to talk to you about this, because my dog acts like this in different situations.) La diamante era muy caro, ya lo comprà ©. (It was very expensive, yet I bought it.)