{"id":310,"date":"2013-02-23T18:45:34","date_gmt":"2013-02-23T18:45:34","guid":{"rendered":"http:\/\/blog.webhostpython.com\/?p=310"},"modified":"2019-09-10T17:53:18","modified_gmt":"2019-09-10T17:53:18","slug":"ruby-programming-adding-strings","status":"publish","type":"post","link":"https:\/\/blog.webhostpython.com\/2013\/02\/23\/ruby-programming-adding-strings\/","title":{"rendered":"Ruby Programming &#8211; Adding Strings"},"content":{"rendered":"<h2><span id=\"String_literals\" class=\"mw-headline\">String literals<\/span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\"><br \/>\n<\/span><\/span><\/h2>\n<p>One way to create a String is to use single or double quotes inside a Ruby program to create what is called a string literal. We&#8217;ve already done this with our &#8220;hello world&#8221; program. A quick update to our code shows the use of both single and double quotes.<\/p>\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"ruby source-ruby\">\n<pre class=\"de1\"><span class=\"kw3\">puts<\/span> <span class=\"st0\">'Hello world'<\/span>\r\n<span class=\"kw3\">puts<\/span> <span class=\"st0\">\"Hello world\"<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>Being able to use either single or double quotes is similar to\u00a0Perl, but different from languages such as\u00a0C\u00a0and\u00a0Java, which use double quotes for string literals and single quotes for single characters.<\/p>\n<p>So what difference is there between single quotes and double quotes in Ruby? In the above code, there&#8217;s no difference. However, consider the following code:<\/p>\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"ruby source-ruby\">\n<pre class=\"de1\"><span class=\"kw3\">puts<\/span> <span class=\"st0\">\"Betty's pie shop\"<\/span>\r\n<span class=\"kw3\">puts<\/span> <span class=\"st0\">'Betty<span class=\"es0\">\\'<\/span>s pie shop'<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>Because &#8220;<code>Betty's<\/code>&#8221; contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is\u00a0<em>in<\/em>\u00a0the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an\u00a0escape sequence.<\/p>\n<h2><span id=\"Single_quotes\" class=\"mw-headline\">Single quotes<\/span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\"><br \/>\n<\/span><\/span><\/h2>\n<p>Single quotes only support two escape sequences.<\/p>\n<ul>\n<li><tt>\\'<\/tt>\u00a0\u2013 single quote<\/li>\n<li><tt>\\\\<\/tt>\u00a0\u2013 single backslash<\/li>\n<\/ul>\n<p>Except for these two escape sequences, everything else between single quotes is treated literally.<\/p>\n<h2><span id=\"Double_quotes\" class=\"mw-headline\">Double quotes<\/span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[<\/span>edit<span class=\"mw-editsection-bracket\">]<\/span><\/span><\/h2>\n<p>Double quotes allow for many more escape sequences than single quotes. They also allow you to embed variables or Ruby code inside of a string literal \u2013 this is commonly referred to asinterpolation.<\/p>\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"ruby source-ruby\">\n<pre class=\"de1\"><span class=\"kw3\">puts<\/span> <span class=\"st0\">\"Enter name\"<\/span>\r\nname = <span class=\"kw3\">gets<\/span>.<span class=\"kw3\">chomp<\/span>\r\n<span class=\"kw3\">puts<\/span> <span class=\"st0\">\"Your name is #{name}\"<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<h3><span id=\"Escape_sequences\" class=\"mw-headline\">Escape sequences<\/span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\"><br \/>\n<\/span><\/span><\/h3>\n<p>Below are some of the more common escape sequences that can appear inside of double quotes.<\/p>\n<ul>\n<li><tt>\\\"<\/tt>\u00a0\u2013 double quote<\/li>\n<li><tt>\\\\<\/tt>\u00a0\u2013 single backslash<\/li>\n<li><tt>\\a<\/tt>\u00a0\u2013\u00a0bell\/alert<\/li>\n<li><tt>\\b<\/tt>\u00a0\u2013\u00a0backspace<\/li>\n<li><tt>\\r<\/tt>\u00a0\u2013\u00a0carriage return<\/li>\n<li><tt>\\n<\/tt>\u00a0\u2013\u00a0newline<\/li>\n<li><tt>\\s<\/tt>\u00a0\u2013\u00a0space<\/li>\n<li><tt>\\t<\/tt>\u00a0\u2013\u00a0tab<\/li>\n<\/ul>\n<p>Try out this example code to better understand escape sequences.<\/p>\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"ruby source-ruby\">\n<pre class=\"de1\"><span class=\"kw3\">puts<\/span> <span class=\"st0\">\"Hello<span class=\"es0\">\\t<\/span><span class=\"es0\">\\t<\/span>world\"<\/span>\r\n \r\n<span class=\"kw3\">puts<\/span> <span class=\"st0\">\"Hello<span class=\"es0\">\\b<\/span><span class=\"es0\">\\b<\/span><span class=\"es0\">\\b<\/span><span class=\"es0\">\\b<\/span><span class=\"es0\">\\b<\/span>Goodbye world\"<\/span>\r\n \r\n<span class=\"kw3\">puts<\/span> <span class=\"st0\">\"Hello<span class=\"es0\">\\r<\/span>Start over world\"<\/span>\r\n \r\n<span class=\"kw3\">puts<\/span> <span class=\"st0\">\"1. Hello<span class=\"es0\">\\n<\/span>2. World\"<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>The result:<\/p>\n<pre>$ <strong>double-quotes.rb<\/strong>\r\nHello\t\tworld\r\nGoodbye world\r\nStart over world\r\n1. Hello\r\n2. World\r\n<\/pre>\n<p>Notice that the newline escape sequence (in the last line of code) simply starts a new line.<\/p>\n<p>The\u00a0bell character, produced by escape code\u00a0<code>\\a<\/code>, is considered a\u00a0control character. It does not represent a letter of the alphabet, a punctuation mark, or any other written symbol. Instead, it instructs the\u00a0terminal emulator\u00a0(called a\u00a0console\u00a0on\u00a0Microsoft Windows) to &#8220;alert&#8221; the user. It is up to the terminal emulator to determine the specifics of how to respond, although a\u00a0beep\u00a0is fairly standard. Some terminal emulators will flash briefly.<\/p>\n<p>Run the following Ruby code to check out how your terminal emulator handles the bell character.<\/p>\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"ruby source-ruby\">\n<pre class=\"de1\"><span class=\"kw3\">puts<\/span> <span class=\"st0\">\"<span class=\"es0\">\\a<\/span>Hello world<span class=\"es0\">\\a<\/span>\"<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<h2><span id=\"puts\" class=\"mw-headline\">puts<\/span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\"><br \/>\n<\/span><\/span><\/h2>\n<p>We&#8217;ve been using the\u00a0<code>puts<\/code>\u00a0function quite a bit to print out text. Whenever\u00a0<code>puts<\/code>\u00a0prints out text, it automatically prints out a newline after the text. For example, try the following code.<\/p>\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"ruby source-ruby\">\n<pre class=\"de1\"><span class=\"kw3\">puts<\/span> <span class=\"st0\">\"Say\"<\/span>, <span class=\"st0\">\"hello\"<\/span>, <span class=\"st0\">\"to\"<\/span>, <span class=\"st0\">\"the\"<\/span>, <span class=\"st0\">\"world\"<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>The result:<\/p>\n<pre>$ <strong>hello-world.rb<\/strong>\r\nSay\r\nhello\r\nto\r\nthe\r\nworld\r\n<\/pre>\n<h2><span id=\"print\" class=\"mw-headline\">print<\/span><span class=\"mw-editsection\"><span class=\"mw-editsection-bracket\">[<\/span>edit<span class=\"mw-editsection-bracket\">]<\/span><\/span><\/h2>\n<p>In contrast, Ruby&#8217;s\u00a0<code>print<\/code>\u00a0function only prints out a newline if you specify one. For example, try out the following code. We include a newline at the end of\u00a0<code>print<\/code>&#8216;s argument list so that the shell prompt appears on a new line, after the text.<\/p>\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"ruby source-ruby\">\n<pre class=\"de1\"> <span class=\"kw3\">print<\/span> <span class=\"st0\">\"Say\"<\/span>, <span class=\"st0\">\"hello\"<\/span>, <span class=\"st0\">\"to\"<\/span>, <span class=\"st0\">\"the\"<\/span>, <span class=\"st0\">\"world\"<\/span>, <span class=\"st0\">\"<span class=\"es0\">\\n<\/span>\"<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>The result:<\/p>\n<pre>$ <strong>hello-world.rb<\/strong>\r\nSayhellototheworld\r\n<\/pre>\n<p>The following code produces the same output, with all the words run together.<\/p>\n<div class=\"mw-geshi mw-code mw-content-ltr\" dir=\"ltr\">\n<div class=\"ruby source-ruby\">\n<pre class=\"de1\"><span class=\"kw3\">print<\/span> <span class=\"st0\">\"Say\"<\/span>\r\n<span class=\"kw3\">print<\/span> <span class=\"st0\">\"hello\"<\/span>\r\n<span class=\"kw3\">print<\/span> <span class=\"st0\">\"to\"<\/span>\r\n<span class=\"kw3\">print<\/span> <span class=\"st0\">\"the\"<\/span>\r\n<span class=\"kw3\">print<\/span> <span class=\"st0\">\"world\"<\/span>\r\n<span class=\"kw3\">print<\/span> <span class=\"st0\">\"<span class=\"es0\">\\n<\/span>\"<\/span><\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>String literals One way to create a String is to use single or double quotes inside a Ruby program to create what is called a string literal. We&#8217;ve already done this with our &#8220;hello world&#8221; program. A quick update to our code shows the use of both single and double quotes. puts &#8216;Hello world&#8217; puts [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":311,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"tdm_status":"","tdm_grid_status":"","footnotes":""},"categories":[45],"tags":[],"class_list":{"0":"post-310","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-ruby-node-js-docker"},"_links":{"self":[{"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/posts\/310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/comments?post=310"}],"version-history":[{"count":0,"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/media\/311"}],"wp:attachment":[{"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.webhostpython.com\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}