{"id":21,"date":"2023-10-31T20:42:18","date_gmt":"2023-07-10T00:08:31","guid":{"rendered":"https:\/\/www.silicloud.com\/zh\/blog\/index.php\/2023\/11\/30\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/"},"modified":"2025-07-31T20:57:41","modified_gmt":"2025-07-31T12:57:41","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/","title":{"rendered":"Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790"},"content":{"rendered":"<h3>\u5f15\u8a00<\/h3>\n<p>\u5b57\u7b26\u4e32\u662f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\u7ec4\u6210\u7684\u5e8f\u5217\uff0c\u8fd9\u4e9b\u5b57\u7b26\u53ef\u4ee5\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u7b26\u53f7\u3002<\/p>\n<p>\u5728Ruby\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u5bf9\u8c61\u3002\u4e0e\u8bb8\u591a\u5176\u4ed6\u8bed\u8a00\u4e0d\u540c\u7684\u662f\uff0cRuby\u4e2d\u7684\u5b57\u7b26\u4e32\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u53ef\u4ee5\u5728\u539f\u5730\u88ab\u4fee\u6539\uff0c\u800c\u65e0\u9700\u521b\u5efa\u65b0\u7684\u5b57\u7b26\u4e32\u5b9e\u4f8b\u3002<\/p>\n<p>\u5728\u60a8\u7f16\u5199\u7684\u51e0\u4e4e\u6bcf\u4e2a\u7a0b\u5e8f\u4e2d\u90fd\u4f1a\u7528\u5230\u5b57\u7b26\u4e32\u3002\u5b57\u7b26\u4e32\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u4fe1\u606f\u5e76\u4e0e\u7528\u6237\u8fdb\u884c\u6587\u672c\u901a\u4fe1\u3002\u5b9e\u9645\u4e0a\uff0c\u60a8\u6b63\u5728\u9605\u8bfb\u7684\u8fd9\u4e2a\u9875\u9762\u5c31\u662f\u7531\u901a\u8fc7\u7f51\u7edc\u6d4f\u89c8\u5668\u5728\u60a8\u7684\u5c4f\u5e55\u4e0a\u663e\u793a\u7684\u5b57\u7b26\u4e32\u6784\u6210\u7684\u3002\u5b57\u7b26\u4e32\u662f\u7f16\u7a0b\u4e2d\u6700\u57fa\u7840\u4e14\u6700\u91cd\u8981\u7684\u6982\u5ff5\u4e4b\u4e00\u3002<\/p>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u5728Ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u3002\u60a8\u5c06\u521b\u5efa\u5b57\u7b26\u4e32\uff0c\u5c06\u5176\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff0c\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff0c\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u8fd8\u5c06\u5b66\u4e60\u5982\u4f55\u5904\u7406\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u6362\u884c\u7b26\u3001\u6487\u53f7\u548c\u53cc\u5f15\u53f7\u3002<\/p>\n<h2>\u521b\u5efa\u5e76\u6253\u5370\u5b57\u7b26\u4e32<\/h2>\n<p>\u5728Ruby\u4e2d\uff0c\u5b57\u7b26\u4e32\u53ef\u4ee5\u5b58\u5728\u4e8e\u5355\u5f15\u53f7 <code>'<\/code> \u6216\u53cc\u5f15\u53f7 <code>\"<\/code> \u4e2d\u3002\u56e0\u6b64\uff0c\u8981\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u53ea\u9700\u5c06\u4e00\u7cfb\u5217\u5b57\u7b26\u5305\u88f9\u5728\u5176\u4e2d\u4e4b\u4e00\u4e2d\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u662f\u4e00\u4e2a\u7528\u5355\u5f15\u53f7\u8868\u793a\u7684\u5b57\u7b26\u4e32\u3002'<\/span><\/span>\r\n<span class=\"token string-literal\"><span class=\"token string\">\"\u8fd9\u662f\u4e00\u4e2a\u7528\u53cc\u5f15\u53f7\u8868\u793a\u7684\u5b57\u7b26\u4e32\u3002\"<\/span><\/span><\/code><\/pre>\n<p>\u60a8\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u60a8\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u5e76\u4e0d\u91cd\u8981\uff0c\u53ea\u8981\u4fdd\u6301\u4e00\u81f4\u5373\u53ef\u3002\u7136\u800c\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u5141\u8bb8\u60a8\u8fdb\u884c\u5b57\u7b26\u4e32\u63d2\u503c\uff0c\u8fd9\u662f\u60a8\u5c06\u5728\u672c\u6559\u7a0b\u540e\u7eed\u90e8\u5206\u5b66\u4e60\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u8981\u5728\u7a0b\u5e8f\u4e2d\u663e\u793a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>print<\/code> \u65b9\u6cd5\u3002<\/p>\n<pre class=\"post-pre\"><code>print <span class=\"token string-literal\"><span class=\"token string\">\"\u8ba9\u6211\u4eec\u6253\u5370\u51fa\u8fd9\u4e2a\u5b57\u7b26\u4e32\u3002\"<\/span><\/span><\/code><\/pre>\n<p><code>print<\/code> \u65b9\u6cd5\u4f1a\u6309\u7167\u539f\u6837\u663e\u793a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8bd5\u4e00\u8bd5\u3002\u5728\u60a8\u7684\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>print.rb<\/code> \u7684\u65b0Ruby\u7a0b\u5e8f\uff0c\u5e76\u4f7f\u7528 <code>print<\/code> \u65b9\u6cd5\u6253\u5370\u4e09\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<div>\u6253\u5370.rb<\/div>\n<pre class=\"post-pre\"><code>print <span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u662f\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002'<\/span><\/span>\r\nprint <span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u662f\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u3002'<\/span><\/span>\r\nprint <span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u662f\u7b2c\u4e09\u4e2a\u5b57\u7b26\u4e32\u3002'<\/span><\/span><\/code><\/pre>\n<p>\u4fdd\u5b58\u6587\u4ef6\u5e76\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">ruby print.rb<\/li><\/ol><\/code><\/pre>\n<p>\u60a8\u4f1a\u770b\u5230\u4ee5\u4e0b\u7684\u8f93\u51fa\uff1a<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>\u8fd9\u662f\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u8fd9\u662f\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u3002\u8fd9\u662f\u7b2c\u4e09\u4e2a\u5b57\u7b26\u4e32\u3002<\/code><\/pre>\n<p>\u4e0d\u662f\u5c06\u4e09\u4e2a\u5b57\u7b26\u4e32\u5206\u522b\u6253\u5370\u5230\u5404\u81ea\u7684\u884c\u4e0a\uff0c\u800c\u662f\u5c06\u8fd9\u4e09\u4e2a\u5b57\u7b26\u4e32\u4e00\u8d77\u6253\u5370\u5230\u540c\u4e00\u884c\u4e0a\u3002<code>print<\/code> \u65b9\u6cd5\u4f1a\u5c06\u5b57\u7b26\u4e32\u8f93\u51fa\u5230\u5c4f\u5e55\u4e0a\uff0c\u4f46\u662f\u5982\u679c\u60a8\u60f3\u8ba9\u6bcf\u4e2a\u5b57\u7b26\u4e32\u663e\u793a\u5728\u72ec\u7acb\u7684\u884c\u4e0a\uff0c\u5c31\u9700\u8981\u81ea\u5df1\u6dfb\u52a0\u6362\u884c\u7b26\u3002<\/p>\n<p>\u5982\u679c\u60a8\u60f3\u8ba9\u8fd9\u4e09\u4e2a\u5b57\u7b26\u4e32\u5206\u522b\u663e\u793a\u5728\u4e0d\u540c\u884c\u4e0a\uff0c\u8bf7\u4f7f\u7528 <code>puts<\/code> \u65b9\u6cd5\u3002\u4fee\u6539\u60a8\u7684\u7a0b\u5e8f\uff0c\u7528 <code>puts<\/code> \u4ee3\u66ff <code>print<\/code>\u3002<\/p>\n<div>\u6253\u5370.rb \u6587\u4ef6<\/div>\n<pre class=\"post-pre\"><code><mark>puts<\/mark> <span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u662f\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002'<\/span><\/span>\r\n<mark>puts<\/mark> <span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u662f\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u3002'<\/span><\/span>\r\n<mark>puts<\/mark> <span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u662f\u7b2c\u4e09\u4e2a\u5b57\u7b26\u4e32\u3002'<\/span><\/span><\/code><\/pre>\n<p>\u73b0\u5728\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u5c31\u4f1a\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>\u8fd9\u662f\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\r\n\u8fd9\u662f\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u3002\r\n\u8fd9\u662f\u7b2c\u4e09\u4e2a\u5b57\u7b26\u4e32\u3002<\/code><\/pre>\n<p><code>puts<\/code> \u65b9\u6cd5\u6253\u5370\u60a8\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\uff0c\u540c\u65f6\u4f1a\u81ea\u52a8\u5728\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\u6dfb\u52a0\u6362\u884c\u7b26\u3002<\/p>\n<h2>\u5c06\u5b57\u7b26\u4e32\u5b58\u50a8\u5230\u53d8\u91cf\u4e2d<\/h2>\n<p>\u53d8\u91cf\u662f\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u4e00\u4e2a\u547d\u540d\u7684\u5f15\u7528\u4f4d\u7f6e\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u53d8\u91cf\u6765\u5b58\u50a8\u6570\u636e\u5e76\u5728\u4ee5\u540e\u68c0\u7d22\u5b83\u3002<\/p>\n<p>\u8981\u5b58\u50a8\u4e00\u4e2a\u5b57\u7b26\u4e32\u5230\u53d8\u91cf\u4e2d\uff0c\u9700\u8981\u5b9a\u4e49\u53d8\u91cf\u540d\u5e76\u8d4b\u4e88\u5b57\u7b26\u4e32\u7684\u503c\u3002<\/p>\n<pre class=\"post-pre\"><code>my_string <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u662f\u6211\u7684\u5b57\u7b26\u4e32'<\/span><\/span><\/code><\/pre>\n<p>\u7136\u540e\uff0c\u8981\u68c0\u7d22\u503c\uff0c\u8bf7\u4f7f\u7528\u53d8\u91cf\u7684\u540d\u79f0\u3002<\/p>\n<pre class=\"post-pre\"><code>print my_string<\/code><\/pre>\n<p>\u8981\u81ea\u5df1\u6d4b\u8bd5\uff0c\u8bf7\u5728\u60a8\u7684\u7f16\u8f91\u5668\u4e2d\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u540d\u4e3a <code>string_variables.rb<\/code> \u7684\u6587\u4ef6\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\"><span class=\"token function\">nano<\/span> string_variables.rb<\/li><\/ol><\/code><\/pre>\n<p>\u7136\u540e\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<div>string_variables.rb \u53d8\u91cf\u4e3a\u5b57\u7b26\u4e32\u7684 Ruby \u6587\u4ef6<\/div>\n<pre class=\"post-pre\"><code>my_name <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"\u9ca8\u9c7c\u8428\u7c73\"<\/span><\/span>\r\nmy_age <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"\u65e0\u53ef\u5949\u544a\"<\/span><\/span>\r\n\r\nputs my_name\r\nputs my_age<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53d8\u91cf\uff1a<code>my_name<\/code> \u548c <code>my_age<\/code>\u3002\u6bcf\u4e2a\u53d8\u91cf\u90fd\u88ab\u8d4b\u4e88\u4e86\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>puts<\/code> \u65b9\u6cd5\u5c06\u6bcf\u4e2a\u5b57\u7b26\u4e32\u5206\u522b\u6253\u5370\u5728\u81ea\u5df1\u7684\u4e00\u884c\u4e0a\u3002<\/p>\n<p>\u4f7f\u7528 <code>Ctrl + X<\/code> \u548c <code>Y<\/code> \u4fdd\u5b58\u6587\u4ef6\uff0c\u7136\u540e\u6267\u884c\u7a0b\u5e8f\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">ruby string_variables.rb<\/li><\/ol><\/code><\/pre>\n<p>\u60a8\u5c06\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>\u9ca8\u9c7c\u8428\u7c73\r\n\u65e0\u53ef\u5949\u544a<\/code><\/pre>\n<p>\u901a\u8fc7\u7ed9\u53d8\u91cf\u8d4b\u503c\u5b57\u7b26\u4e32\uff0c\u60a8\u53ef\u4ee5\u907f\u514d\u6bcf\u6b21\u4f7f\u7528\u5b57\u7b26\u4e32\u65f6\u90fd\u8981\u91cd\u590d\u8f93\u5165\u76f8\u540c\u7684\u5b57\u7b26\u4e32\uff0c\u4ece\u800c\u66f4\u8f7b\u677e\u5730\u5904\u7406\u548c\u64cd\u4f5c\u7a0b\u5e8f\u4e2d\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u521b\u5efa\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<h2>\u5b57\u7b26\u4e32\u8fde\u63a5 (String Concatenation)<\/h2>\n<p>\u8fde\u63a5\u610f\u5473\u7740\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u4e3a\u4e86\u8fdb\u884c\u8fde\u63a5\uff0c\u6211\u4eec\u4f7f\u7528\u8fde\u63a5\u8fd0\u7b97\u7b26\uff0c\u8868\u793a\u4e3a\u52a0\u53f7\uff08<code>+<\/code>\uff09\u7b26\u53f7\u3002\u5f53\u7528\u4e8e\u7b97\u672f\u8fd0\u7b97\u65f6\uff0c\u52a0\u53f7\u7b26\u53f7\u4e5f\u662f\u52a0\u6cd5\u8fd0\u7b97\u7b26\u3002<\/p>\n<p>\u8fd9\u662f\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u201csammy\u201d\u548c\u201cshark\u201d\u8fde\u63a5\u5728\u4e00\u8d77\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"sammy\"<\/span><\/span> <span class=\"token operator\">+<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"shark\"<\/span><\/span><\/code><\/pre>\n<p>\u8fd9\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>sammyshark<\/code><\/pre>\n<p>\u8fde\u63a5\u5c06\u5b57\u7b26\u4e32\u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u5c06\u5b83\u4eec\u5408\u5e76\u5e76\u8f93\u51fa\u4e00\u4e2a\u5168\u65b0\u7684\u5b57\u7b26\u4e32\u503c\u3002\u5982\u679c\u60a8\u60f3\u5728\u5355\u8bcd\u201csammy\u201d\u548c\u201cshark\u201d\u4e4b\u95f4\u6dfb\u52a0\u7a7a\u683c\uff0c\u60a8\u9700\u8981\u5728\u5176\u4e2d\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u8be5\u7a7a\u683c\uff0c\u50cf\u8fd9\u6837:<\/p>\n<pre class=\"post-pre\"><code>\"sammy \" + \"shark\"<\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u5728\u7a0b\u5e8f\u4e2d\u60a8\u4e0d\u4f1a\u771f\u7684\u50cf\u8fd9\u6837\u5199\u4ee3\u7801\uff0c\u4f46\u60a8\u7ecf\u5e38\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u6df7\u5408\u5728\u4e00\u8d77\uff0c\u8fd9\u5c31\u662f\u62fc\u63a5\u7684\u7528\u6b66\u4e4b\u5730\u3002<\/p>\n<p>\u8fd9\u91cc\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"post-pre\"><code>color <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"\u84dd\u8272\"<\/span><\/span>\r\nprint <span class=\"token string-literal\"><span class=\"token string\">\"\u6211\u6700\u559c\u6b22\u7684\u989c\u8272\u662f \"<\/span><\/span> <span class=\"token operator\">+<\/span> color<\/code><\/pre>\n<p>\u8fd9\u5c06\u5bfc\u81f4\u8f93\u51fa\u4e3a\u201c\u6211\u6700\u559c\u6b22\u7684\u989c\u8272\u662f \u84dd\u8272\u201d\u3002\u8bf7\u6ce8\u610f\uff0c\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u5355\u8bcd\u201c\u662f\u201d\u540e\u9762\u7559\u6709\u4e00\u4e2a\u7a7a\u683c\uff0c\u8fd9\u6837\u8f93\u51fa\u4e2d\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u503c\u4e4b\u95f4\u5c31\u4f1a\u6709\u4e00\u4e2a\u7a7a\u683c\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u5c06\u591a\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u521b\u5efa\u6587\u4ef6 <code>concatenation.rb<\/code>\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<div>\u5c06\u4ee5\u4e0b\u5185\u5bb9\u4ee5\u4e2d\u6587\u672c\u5730\u5316\u6392\u5217\u62fc\u63a5.rb<\/div>\n<p>\u8fd9\u662f\u6587\u7ae0\u300a\u5982\u4f55\u5728Ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u300b\u7684\u7b2c2\u90e8\u5206\uff08\u51715\u90e8\u5206\uff09\u3002<\/p>\n<pre class=\"post-pre\"><code class=\"language-ruby\">my_name = \"Sammy the Shark\"\r\nmy_age = \"none of your business\"\r\n\r\nputs \"My name is \" + my_name + \" and my age is \" + my_age + \".\"\r\n<\/code><\/pre>\n<p>\u6b64\u7a0b\u5e8f\u5b9a\u4e49\u4e86\u4e24\u4e2a\u53d8\u91cf\uff1a<code>my_name<\/code> \u548c <code>my_age<\/code>\uff0c\u6bcf\u4e2a\u53d8\u91cf\u90fd\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5c31\u50cf\u60a8\u4e4b\u524d\u64cd\u4f5c\u7684\u90a3\u6837\u3002\u4f46\u8fd9\u4e00\u6b21\uff0c\u6211\u4eec\u4e0d\u662f\u76f4\u63a5\u6253\u5370\u53d8\u91cf\u7684\u503c\uff0c\u800c\u662f\u901a\u8fc7\u5b57\u7b26\u4e32\u8fde\u63a5\uff08Concatenation\uff09\u5c06\u8fd9\u4e9b\u503c\u4e0e\u4e00\u4e9b\u4e0a\u4e0b\u6587\u4fe1\u606f\u4e00\u8d77\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p>\u5f53\u60a8\u8fd0\u884c\u6b64\u7a0b\u5e8f\u65f6\uff0c\u60a8\u4f1a\u770b\u5230\u4ee5\u4e0b\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"post-pre\"><code class=\"language-output\"><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>My name is Sammy the Shark and my age is none of your business.\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u5c0f\u7a0b\u5e8f\u4e2d\uff0c\u60a8\u4f7f\u7528\u4e86\u5b57\u7b26\u4e32\u8fde\u63a5\u5c06\u53d8\u91cf\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<p>\u5f53\u60a8\u901a\u8fc7\u8fde\u63a5\u5c06\u4e24\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\u4e32\u7ec4\u5408\u65f6\uff0c\u60a8\u6b63\u5728\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u60a8\u53ef\u4ee5\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5b83\u3002\u56e0\u6b64\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5c06\u521b\u5efa\u7684\u5b57\u7b26\u4e32\u5206\u914d\u7ed9\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u4ee5\u4fbf\u5c06\u6765\u4f7f\u7528\u3002<\/p>\n<div class=\"filename\">\u8fde\u63a5.rb<\/div>\n<pre class=\"post-pre\"><code class=\"language-ruby\">my_name = \"Sammy the Shark\"\r\nmy_age = \"none of your business\"\r\n\r\n# \u5c06\u8fde\u63a5\u540e\u7684\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u53d8\u91cf\r\noutput = \"My name is \" + my_name + \" and my age is \" + my_age + \".\"\r\n\r\n# \u6253\u5370\u8f93\u51fa\u3002\r\nputs output\r\n<\/code><\/pre>\n<p>\u5728\u50cf\u8fd9\u6837\u7684\u5c0f\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u989d\u5916\u7684\u8f93\u51fa\u53d8\u91cf\u53ef\u80fd\u4e0d\u662f\u5fc5\u9700\u7684\u3002\u4f46\u5728\u8f83\u5927\u7684\u7a0b\u5e8f\u4e2d\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528\u8fde\u63a5\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u5728\u591a\u4e2a\u5730\u65b9\u4f7f\u7528\u8be5\u5b57\u7b26\u4e32\u3002\u6b64\u5916\uff0c\u5c06\u6570\u636e\u5904\u7406\uff08\u5982\u8fde\u63a5\u548c\u7b97\u672f\u8fd0\u7b97\uff09\u4e0e\u8f93\u51fa\u5206\u79bb\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\uff0c\u56e0\u4e3a\u968f\u7740\u7a0b\u5e8f\u7684\u6269\u5c55\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5c06\u903b\u8f91\u548c\u8f93\u51fa\u5206\u79bb\u5230\u4e0d\u540c\u7684\u6587\u4ef6\u6216\u7ec4\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u7ba1\u7406\u3002<\/p>\n<p>\u8bf7\u786e\u4fdd\u4e0d\u8981\u5728\u4e24\u79cd\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u4f7f\u7528 <code>+<\/code> \u8fd0\u7b97\u7b26\u3002\u4f8b\u5982\uff0c\u60a8\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32\u548c\u6574\u6570\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<p>\u4e3a\u4e86\u4e86\u89e3\u4f1a\u53d1\u751f\u4ec0\u4e48\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>\u5b57\u7b26\u4e32\u4e0e\u6574\u6570.rb<\/code> \u7684\u65b0\u7a0b\u5e8f\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<div class=\"filename\">\u5b57\u7b26\u4e32\u4e0e\u6574\u6570.rb<\/div>\n<pre class=\"post-pre\"><code class=\"language-ruby\">my_name = \"Sammy the Shark\"\r\nmy_number = 27\r\n\r\nprint my_name + my_number\r\n<\/code><\/pre>\n<p>\u8fd9\u6b21\u6211\u4eec\u6709\u4e00\u4e2a\u53d8\u91cf <code>my_name<\/code>\uff0c\u5b83\u5b58\u50a8\u4e86\u5b57\u7b26\u4e32 &#8220;Sammy the Shark&#8221;\uff0c\u8fd8\u6709\u4e00\u4e2a\u53d8\u91cf <code>my_number<\/code>\uff0c\u5b83\u5b58\u50a8\u4e86\u6574\u6570 27\u3002\u6211\u4eec\u77e5\u9053 27 \u4e0d\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u88ab\u5f15\u53f7\u5305\u56f4\u3002\u5b83\u4e5f\u6ca1\u6709\u5c0f\u6570\u70b9\uff0c\u6240\u4ee5\u6211\u4eec\u77e5\u9053\u5b83\u662f\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<p>\u5982\u679c\u60a8\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"post-pre\"><code class=\"language-bash\"><ol><li data-prefix=\"$\">ruby \u5b57\u7b26\u4e32\u4e0e\u6574\u6570.rb<\/li><\/ol>\r\n<\/code><\/pre>\n<p>\u60a8\u4f1a\u770b\u5230\u8fd9\u4e2a\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<pre class=\"post-pre\"><code class=\"language-output\"><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>\u5b57\u7b26\u4e32\u4e0e\u6574\u6570.rb:4:in `+': no implicit conversion of Integer into String (TypeError)\r\nfrom \u5b57\u7b26\u4e32\u4e0e\u6574\u6570.rb:4:in `&lt;main&gt;'\r\n<\/code><\/pre>\n<p>\u201c\u9519\u8bef\u7684\u9690\u5f0f\u8f6c\u6362\u6574\u6570\u5230\u5b57\u7b26\u4e32\u201d\u7684\u610f\u601d\u662f\uff0cRuby \u53ea\u80fd\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u5230\u73b0\u6709\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<div class=\"post-conf-note\">\n<p class=\"post-note\">\n<p class=\"post-conf-desc\">\u6ce8\u610f<\/p>\n<p>\u5728 Ruby \u7248\u672c 2.3 \u53ca\u4ee5\u4e0b\uff0c\u60a8\u4f1a\u770b\u5230\u4ee5\u4e0b\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n<p><code>\u5b57\u7b26\u4e32\u4e0e\u6574\u6570.rb:4:in `+': no implicit conversion of Fixnum into String (TypeError)<\/code><br \/><code>from \u5b57\u7b26\u4e32\u4e0e\u6574\u6570.rb:4:in `&lt;main&gt;'<\/code><\/p>\n<p>\u5728\u4ee5\u524d\u7684 Ruby \u7248\u672c\u4e2d\uff0c\u5173\u952e\u5b57 <code>Fixnum<\/code> \u88ab\u8d4b\u4e88\u7ed9\u6574\u6570\u7684\u6570\u636e\u7c7b\u578b\u3002\u5b83\u662f Fixed Number \u7684\u7b80\u79f0\u3002\u5728 Ruby 2.4 \u4e2d\uff0c<code>Fixnum<\/code> \u53ca\u5176\u5bf9\u5e94\u7684 <code>Bignum<\/code> \u4e0d\u518d\u5b58\u5728\uff0c\u800c\u662f\u88ab <code>Integer<\/code> \u66ff\u4ee3\u3002<\/p>\n<\/div>\n<p>\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u6211\u4eec\u7684\u7a0b\u5e8f\uff0c\u5c06\u6570\u5b57 27 \u653e\u5728\u5f15\u53f7\u4e2d\uff08&#8221;27&#8243;\uff09\uff0c\u8fd9\u6837\u5b83\u5c31\u88ab\u58f0\u660e\u4e3a\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u6574\u6570\u3002\u6216\u8005\u6211\u4eec\u53ef\u4ee5\u5728\u521b\u5efa\u5b57\u7b26\u4e32\u65f6\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<div class=\"filename\">\u5b57\u7b26\u4e32\u4e0e\u6574\u6570.rb<\/div>\n<pre class=\"post-pre\"><code class=\"language-ruby\">my_name = \"Sammy the Shark\"\r\nmy_number = 27\r\n\r\nprint my_name + my_number<mark>.to_s<\/mark>\r\n<\/code><\/pre>\n<p><code>.to_s<\/code> \u65b9\u6cd5\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd9\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4f7f\u6211\u4eec\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u4fdd\u6301\u6570\u5b57\u7684\u6574\u4f53\u6027\u3002\u6211\u4eec\u53ea\u9700\u8981\u5728\u6253\u5370\u65f6\u5c06\u5176\u4f5c\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f46\u5982\u679c\u6211\u4eec\u9700\u8981\u5728\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u903b\u8f91\u4e2d\u4f7f\u7528\u5b83\u4f5c\u4e3a\u6574\u6570\uff0c\u53ef\u80fd\u5e0c\u671b\u5b83\u662f\u4e00\u4e2a\u6574\u6570\u3002<\/p>\n<p>\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u60a8\u5c06\u5728\u5c4f\u5e55\u4e0a\u770b\u5230 &#8220;Sammy the Shark27&#8221; \u7684\u8f93\u51fa\u3002<\/p>\n<p>\u5f53\u6d89\u53ca\u5230\u90ae\u653f\u7f16\u7801\u3001\u8d27\u5e01\u3001\u7535\u8bdd\u53f7\u7801\u4ee5\u53ca\u5176\u4ed6\u60a8\u60f3\u8981\u5728\u5c4f\u5e55\u4e0a\u4e0e\u6587\u672c\u4e00\u8d77\u663e\u793a\u7684\u6570\u5b57\u6570\u636e\u65f6\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u5b57\u7b26\u4e32\u8fdb\u884c\u8fde\u63a5\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u8fde\u63a5\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u4e5f\u53ef\u80fd\u5f88\u68d8\u624b\u3002\u5982\u679c\u60a8\u4e0d\u5c0f\u5fc3\u6f0f\u6389\u4e00\u4e2a <code>+<\/code> \u8fd0\u7b97\u7b26\uff0c\u5c31\u4f1a\u51fa\u73b0\u8bed\u6cd5\u9519\u8bef\u3002\u800c\u4e14\u5982\u679c\u60a8\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u4e0e\u4fdd\u5b58\u6570\u5b57\u7684\u53d8\u91cf\u8fde\u63a5\u8d77\u6765\uff0c\u60a8\u8fd8\u9700\u8981\u5c06\u53d8\u91cf\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002Ruby \u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u5c06\u53d8\u91cf\u503c\u63d2\u5165\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff0c\u79f0\u4e3a\u5b57\u7b26\u4e32\u63d2\u503c\uff0c\u5b83\u89e3\u51b3\u4e86\u8fd9\u4e24\u4e2a\u95ee\u9898\u3002<\/p>\n<h2>\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u503c<\/h2>\n<p>\u5f53\u8fde\u63a5\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u65f6\uff0c\u8f93\u51fa\u53ef\u80fd\u5f88\u96be\u9605\u8bfb\u548c\u8c03\u8bd5\u3002\u5b57\u7b26\u4e32\u63d2\u503c\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u5b83\u5141\u8bb8\u60a8\u5728\u53cc\u5f15\u53f7\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<p>\u4e0d\u8981\u8fd9\u6837\u5199\uff1a<\/p>\n<pre class=\"post-pre\"><code class=\"language-ruby\">\"My name is \" + my_name + \"!\"\r\n<\/code><\/pre>\n<p>\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<p>\u8fd9\u662f\u6587\u7ae0\u300a\u5982\u4f55\u5728Ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u300b\u7684\u7b2c3\u90e8\u5206\uff08\u51715\u90e8\u5206\uff09\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"\u6211\u7684\u540d\u5b57\u662f <\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">my_name<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\">!\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u4e0d\u4f7f\u7528\u7ec8\u6b62\u5b57\u7b26\u4e32\u548c\u52a0\u53f7\u64cd\u4f5c\u7b26\uff0c\u800c\u662f\u4f7f\u7528 <code>#{}<\/code> \u8bed\u6cd5\u5c06\u53d8\u91cf\u62ec\u8d77\u6765\u3002\u8fd9\u79cd\u8bed\u6cd5\u544a\u8bc9 Ruby \u5bf9\u8868\u8fbe\u5f0f\u8fdb\u884c\u6c42\u503c\uff0c\u5e76\u5c06\u5176\u6ce8\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\u3002<\/p>\n<p>\u8bd5\u4e00\u8bd5\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>interpolation.rb<\/code> \u7684\u65b0\u7a0b\u5e8f\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<div>\u5728 <code>.rb<\/code> \u6587\u4ef6\u4e2d\u8fdb\u884c\u63d2\u503c\u3002<\/div>\n<pre class=\"post-pre\"><code>my_name <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"Sammy the Shark\"<\/span><\/span>\r\nmy_age <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"\u65e0\u53ef\u5949\u544a\"<\/span><\/span>\r\n\r\noutput <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"\u6211\u7684\u540d\u5b57\u662f <\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">my_name<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\">\uff0c\u6211\u7684\u5e74\u9f84\u662f <\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">my_age<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\">\u3002\"<\/span><\/span>\r\n\r\nputs output\r\n<\/code><\/pre>\n<p>\u8fd9\u662f\u4f60\u4e4b\u524d\u5199\u8fc7\u7684\u540c\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u4f46\u8fd9\u6b21\u6211\u4eec\u4f1a\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u503c\u6765\u521b\u5efa\u8f93\u51fa\u3002<\/p>\n<p>\u5b57\u7b26\u4e32\u63d2\u503c\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\uff1a\u5b83\u53ef\u4ee5\u81ea\u52a8\u5c06\u6570\u503c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u8fd8\u8bb0\u5f97\u4f60\u7684 <code>strings_and_integers.rb<\/code> \u7a0b\u5e8f\u5417\uff1f\u518d\u6b21\u6253\u5f00\u4f60\u7684\u7f16\u8f91\u5668\u5e76\u5c06\u6700\u540e\u4e00\u884c\u6539\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<div>\u5b57\u7b26\u4e32\u548c\u6574\u6570.rb<\/div>\n<pre class=\"post-pre\"><code>my_name <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"Sammy the Shark\"<\/span><\/span>\r\nmy_number <span class=\"token operator\">=<\/span> <span class=\"token number\">27<\/span>\r\n\r\n<span class=\"token comment\"># \u4f7f\u7528\u63d2\u503c\u800c\u975e\u8fde\u63a5<\/span>\r\nprint <span class=\"token string-literal\"><span class=\"token string\">\"\u6211\u7684\u540d\u5b57\u662f <\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">my_name<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\">\uff0c\u6211\u6700\u559c\u6b22\u7684\u6570\u5b57\u662f <\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">my_number<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\">\u3002\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u5f53\u4f60\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0cRuby \u5c06\u4f1a\u81ea\u52a8\u5c06 <code>my_number<\/code> \u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14\u4f60\u7684\u7a0b\u5e8f\u5c06\u4f1a\u6253\u5370\u51fa\u4ee5\u4e0b\u8f93\u51fa\u3002<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>\u6211\u7684\u540d\u5b57\u662f Sammy the Shark\uff0c\u6211\u6700\u559c\u6b22\u7684\u6570\u5b57\u662f 27\u3002\r\n<\/code><\/pre>\n<p>\u5b57\u7b26\u4e32\u63d2\u503c\u975e\u5e38\u5f3a\u5927\u4e14\u65b9\u4fbf\u3002\u5b83\u4e5f\u662f\u4e0e\u53d8\u91cf\u8fde\u63a5\u5b57\u7b26\u4e32\u7684\u9996\u9009\u65b9\u6cd5\u3002<\/p>\n<h2>\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u548c\u5b57\u7b26\u4e32\u503c<\/h2>\n<p>\u8bf7\u6ce8\u610f\uff0c\u60a8\u5728\u4ee3\u7801\u4e2d\u521b\u5efa\u7684\u6240\u6709\u5b57\u7b26\u4e32\u90fd\u5305\u542b\u5728\u5f15\u53f7\u4e2d\uff0c\u4f46\u5b9e\u9645\u8f93\u51fa\u7684\u7ed3\u679c\u4e0d\u5305\u542b\u5f15\u53f7\u3002<\/p>\n<p>\u5728\u63d0\u53ca\u6bcf\u4e2a\u65f6\uff0c\u5b83\u4eec\u4e4b\u95f4\u6709\u533a\u522b\u3002\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u662f\u5728\u6e90\u4ee3\u7801\u4e2d\u6240\u5199\u7684\u5b57\u7b26\u4e32\uff0c\u5305\u62ec\u5f15\u53f7\u3002\u5b57\u7b26\u4e32\u503c\u662f\u60a8\u5728\u8f93\u51fa\u4e2d\u770b\u5230\u7684\u5185\u5bb9\uff0c\u4e0d\u5305\u542b\u5f15\u53f7\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"Sammy the Shark\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u5b57\u7b26\u4e32\u7684\u503c\u5c06\u662f Sammy the Shark\u3002<\/p>\n<p>\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u60a8\u4e0d\u5fc5\u62c5\u5fc3\u8fd9\u4e2a\u533a\u522b\uff0c\u9664\u975e\u60a8\u60f3\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u7279\u6b8a\u5b57\u7b26\uff0c\u4f8b\u5982\u5f15\u53f7\u6216\u6487\u53f7\u3002<\/p>\n<h2>\u5728\u5b57\u7b26\u4e32\u4e2d\u8f6c\u4e49\u5f15\u53f7\u548c\u6487\u53f7<\/h2>\n<p>\u7531\u4e8e\u5f15\u53f7\u7528\u4e8e\u8868\u793a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u4f60\u60f3\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u6487\u53f7\u548c\u5f15\u53f7\uff0c\u4f60\u9700\u8981\u989d\u5916\u7684\u52aa\u529b\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5728\u5355\u5f15\u53f7\u5b57\u7b26\u4e32\u7684\u4e2d\u95f4\u5c1d\u8bd5\u4f7f\u7528\u6487\u53f7\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u4e0d\u662f\u6211\u60f3\u8981\u7684\u3002'<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ef\u4ee5\u4ece\u5947\u602a\u7684\u9ad8\u4eae\u663e\u793a\u4e2d\u770b\u51fa\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u82f1\u6587\u6487\u53f7 <code>'<\/code> \u6ca1\u6709\u7ed3\u675f\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0cRuby \u89e3\u91ca\u5668\u5c06\u5c1d\u8bd5\u89e3\u6790\u5269\u4e0b\u7684\u610f\u56fe\u5b57\u7b26\u4e32\uff0c\u800c\u4f60\u5c06\u4f1a\u5f97\u5230\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u88ab\u53cc\u5f15\u53f7\u5305\u56f4\u7684\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u53cc\u5f15\u53f7\uff0c\u4f60\u4f1a\u9047\u5230\u76f8\u540c\u7684\u60c5\u51b5\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"Sammy \u8bf4\uff0c\"<\/span><\/span>\u4f60\u597d<span class=\"token operator\">!<\/span><span class=\"token string-literal\"><span class=\"token string\">\"\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Hello<\/code> \u524d\u9762\u7684\u53f3\u53cc\u5f15\u53f7\u7ec8\u6b62\u4e86\u5b57\u7b26\u4e32\uff0c\u5e76\u4e14 <code>Hello!<\/code> \u4e4b\u540e\u7684\u53cc\u5f15\u53f7\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u5b83\u6ca1\u6709\u5339\u914d\u7684\u53f3\u53cc\u5f15\u53f7\u6765\u7ec8\u6b62\u5b83\uff0c\u56e0\u6b64 Ruby \u5c06\u663e\u793a\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u4f60\u6709\u51e0\u4e2a\u9009\u9879\u3002\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u66ff\u4ee3\u8bed\u6cd5\u6765\u521b\u5efa\u5b57\u7b26\u4e32\uff1b\u5982\u679c\u4f60\u5fc5\u987b\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u53cc\u5f15\u53f7\uff0c\u90a3\u4e48\u7528\u5355\u5f15\u53f7\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\uff0c\u53cd\u4e4b\u4ea6\u7136\u3002\u4f60\u4e5f\u53ef\u4ee5\u5bf9\u5f15\u53f7\u8fdb\u884c\u8f6c\u4e49\uff0c\u6216\u8005\u4f7f\u7528\u4e0d\u540c\u7684 Ruby \u8bed\u6cd5\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\u3002\u8ba9\u6211\u4eec\u6765\u770b\u770b\u6bcf\u79cd\u65b9\u6cd5\u3002<\/p>\n<h3>\u9009\u98791\uff1a\u4f7f\u7528\u5907\u7528\u5b57\u7b26\u4e32\u8bed\u6cd5\u3002<\/h3>\n<p>\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\uff1a\u5f53\u4f60\u7684\u5b57\u7b26\u4e32\u91cc\u9762\u9700\u8981\u5305\u542b\u53cc\u5f15\u53f7\u65f6\uff0c\u7528\u5355\u5f15\u53f7\u5c06\u5b57\u7b26\u4e32\u62ec\u8d77\u6765\uff1b\u5f53\u4f60\u7684\u5b57\u7b26\u4e32\u9700\u8981\u4f7f\u7528\u5355\u5f15\u53f7\u65f6\uff0c\u7528\u53cc\u5f15\u53f7\u5c06\u5b57\u7b26\u4e32\u62ec\u8d77\u6765\u3002<\/p>\n<p>\u4e0d\u8981\u4f7f\u7528\u5355\u5f15\u53f7\u5b9a\u4e49\u8fd9\u4e2a\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">'\u8fd9\u4e0d\u662f\u6211\u60f3\u8981\u7684\u3002'<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u7528\u53cc\u5f15\u53f7\u6765\u5b9a\u4e49\u5b83\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"\u8fd9\u4e0d\u662f\u6211\u60f3\u8981\u7684\u3002\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u76f8\u53cd\uff0c\u4e0d\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u6765\u5b9a\u4e49\u8fd9\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"Sammy \u8bf4\uff0c\"<\/span><\/span>\u4f60\u597d<span class=\"token operator\">!<\/span><span class=\"token string-literal\"><span class=\"token string\">\"\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u8bf7\u4f7f\u7528\u5355\u5f15\u53f7\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">'Sammy \u8bf4\uff0c\u201c\u4f60\u597d\uff01\u201d'<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u4f7f\u7528\u7684\u5907\u9009\u8bed\u6cd5\u53ef\u4ee5\u89e3\u51b3\u4e00\u4e9b\u7d27\u6025\u56f0\u5883\uff0c\u4f46\u5e76\u4e0d\u603b\u662f\u9002\u7528\u3002\u4f8b\u5982\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u65e2\u4e0d\u80fd\u901a\u8fc7\u4efb\u4f55\u4e00\u79cd\u65b9\u6cd5\u89e3\u51b3\u3002<\/p>\n<p>\u8fd9\u662f\u6587\u7ae0\u300a\u5982\u4f55\u5728Ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u300b\u7684\u7b2c4\u90e8\u5206\uff08\u51715\u90e8\u5206\uff09\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"Sammy says, \"<\/span><\/span><span class=\"token constant\">I<\/span>'m a happy shark<span class=\"token operator\">!<\/span><span class=\"token string-literal\"><span class=\"token string\">\"\"<\/span><\/span><\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u524d\u9762\u7684\u7ed3\u5c3e\u53cc\u5f15\u53f7<code>\"<\/code>\u5728<code>I'm<\/code>\u4e4b\u524d\u5bfc\u81f4\u4e86\u6df7\u4e71\u3002\u5b83\u7ec8\u6b62\u4e86\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540eRuby\u9047\u5230\u4e86<code>I'm<\/code>\u4e2d\u7684\u6487\u53f7\uff0c\u8fd9\u5c06\u5f00\u59cb\u4e00\u4e2a\u65b0\u7684\u503c\u4e3a<code>\"\u6211\u662f\u4e00\u53ea\u5f00\u5fc3\u7684\u9ca8\u9c7c\uff01\"\"<\/code>\u7684\u5b57\u7b26\u4e32\u3002\u4f46\u662f\u8fd9\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u6ca1\u6709\u5339\u914d\u7684\u5355\u5f15\u53f7\u6765\u7ec8\u6b62\u5b83\u3002\u800c\u4f7f\u7528\u5355\u5f15\u53f7\u6765\u5305\u542b\u5b57\u7b26\u4e32\u5f15\u5165\u4e86\u4e00\u4e2a\u7c7b\u4f3c\u7684\u95ee\u9898\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">'Sammy says, \"I'<\/span><\/span>m a happy shark<span class=\"token operator\">!<\/span>\"'<\/code><\/pre>\n<p>\u8fd9\u6b21<code>\"I'm\"<\/code>\u4e2d\u7684\u6487\u53f7\u7ec8\u7ed3\u4e86\u8fd9\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4f7f\u7528\u66ff\u4ee3\u8bed\u6cd5\u4e5f\u4f1a\u4f7f\u60a8\u7684\u4ee3\u7801\u4e0d\u4e00\u81f4\u3002\u5728\u5b57\u7b26\u4e32\u8bed\u6cd5\u4e4b\u95f4\u4e0d\u65ad\u5207\u6362\u53ef\u80fd\u4f1a\u4ee4\u4eba\u56f0\u60d1\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8f6c\u4e49\u5b57\u7b26\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<h3>\u9009\u98792\uff1a\u5b57\u7b26\u4e32\u4e2d\u7684\u8f6c\u4e49\u5b57\u7b26\u8f6c\u6362<\/h3>\n<p>\u53cd\u659c\u6760\u5b57\u7b26\uff08<code>\\<\/code>\uff09\uff0c\u901a\u5e38\u5728\u5b57\u7b26\u4e32\u4e2d\u88ab\u79f0\u4e3a\u8f6c\u4e49\u5b57\u7b26\uff0c\u53ef\u4ee5\u9632\u6b62Ruby\u76f4\u63a5\u89e3\u91ca\u5b57\u7b26\u4e32\u4e2d\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<p>\u8fd9\u662f\u6211\u4eec\u6709\u95ee\u9898\u7684\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528\u53cc\u5f15\u53f7\u8fdb\u884c\u7f16\u7801\uff0c\u5e76\u4e14\u5d4c\u5957\u4e86\u53cc\u5f15\u53f7\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"Sammy says, \"<\/span><\/span><span class=\"token constant\">I<\/span>'m a happy shark<span class=\"token operator\">!<\/span><span class=\"token string-literal\"><span class=\"token string\">\"\"<\/span><\/span><\/code><\/pre>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>quoting.rb<\/code>\u7684\u65b0\u7684Ruby\u7a0b\u5e8f\uff0c\u5e76\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230\u6587\u4ef6\u4e2d\u3002<\/p>\n<div>\u5f15\u7528.rb<\/div>\n<pre class=\"post-pre\"><code>print <span class=\"token string-literal\"><span class=\"token string\">\"Sammy says, \"<\/span><\/span><span class=\"token constant\">I<\/span>'m a happy shark<span class=\"token operator\">!<\/span><span class=\"token string-literal\"><span class=\"token string\">\"\"<\/span><\/span><\/code><\/pre>\n<p>\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<pre class=\"post-pre\"><code><ol><li data-prefix=\"$\">ruby quoting.rb<\/li><\/ol><\/code><\/pre>\n<p>\u7136\u540e\u4f60\u4f1a\u770b\u5230\u8fd9\u4e2a\u7ed3\u679c\u3002<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">Outputquoting.rb:1: syntax error, unexpected tCONSTANT, expecting end-of-input<br>print \"Sammy says, \"I'm a happy shark!\"\"<br>^<\/div><\/code><\/pre>\n<p>\u4e3a\u4e86\u4fee\u590d\u9519\u8bef\uff0c\u9700\u8981\u5728\u5185\u90e8\u53cc\u5f15\u53f7\u524d\u4f7f\u7528\u53cd\u659c\u6760\u3002<\/p>\n<div>\u5f15\u7528.rb<\/div>\n<pre class=\"post-pre\"><code>print <span class=\"token string-literal\"><span class=\"token string\">\"Sammy says, \\\"I'm a happy shark!\\\"\"<\/span><\/span><\/code><\/pre>\n<p>\u7136\u540e\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u4f60\u4f1a\u770b\u5230\u4f60\u9884\u671f\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<pre class=\"post-pre\"><code>Sammy says, \"I'm a happy shark!\"<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u4f60\u4e0d\u5fc5\u8f6c\u4e49\u6487\u53f7\uff0c\u56e0\u4e3a\u6ca1\u6709\u51b2\u7a81\u3002\u4f60\u53ea\u9700\u8981\u8f6c\u4e49\u90a3\u4e9b\u4f1a\u8ba9Ruby\u6df7\u6dc6\u7684\u5f15\u53f7\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u8bed\u6cd5\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\u6765\u5b8c\u5168\u907f\u514d\u8f6c\u4e49\u5f15\u53f7\u3002<\/p>\n<h3>\u9009\u98793\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u66ff\u4ee3\u8bed\u6cd5<\/h3>\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u60a8\u4f7f\u7528\u5f15\u53f7\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\u7684\u8fb9\u754c\u3002\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\u6765\u521b\u5efaRuby\u4e2d\u7684\u5b57\u7b26\u4e32\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u767e\u5206\u53f7\u540e\u6307\u5b9a\u5b83\u6765\u5b9a\u4e49\u5b9a\u754c\u7b26\u6216\u5305\u56f4\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">%$Sammy says, \"I'm a happy shark!\"$<\/span><\/span><\/code><\/pre>\n<p>\u8fd9\u4e2a\u8bed\u6cd5\u4f1a\u81ea\u52a8\u4e3a\u60a8\u8f6c\u4e49\u5d4c\u5165\u7684\u5b57\u7b26\u4e32\u3002\u5b9e\u9645\u7684\u5b57\u7b26\u4e32\u770b\u8d77\u6765\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"post-pre\"><code>\"Sammy says, \\\"I'm a happy shark!\\\"\"<\/code><\/pre>\n<p>\u7136\u800c\uff0c\u6539\u53d8\u5206\u9694\u7b26\u610f\u5473\u7740\u5982\u679c\u9700\u8981\u4f7f\u7528\u5b83\uff0c\u4f60\u5c31\u5fc5\u987b\u5bf9\u5206\u9694\u7b26\u8fdb\u884c\u8f6c\u4e49\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u5fc5\u987b\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u7f8e\u5143\u7b26\u53f7\uff0c\u4f60\u9700\u8981\u8f6c\u4e49\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u9762\u7f8e\u5143\u7b26\u53f7\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e00\u5bf9\u5927\u62ec\u53f7\u3001\u65b9\u62ec\u53f7\u6216\u8005\u5706\u62ec\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u5927\u62ec\u53f7\u662f\u6700\u5e38\u89c1\u7684\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">%{Sammy says, \"I'm a happy shark!\"}<\/span><\/span><\/code><\/pre>\n<p>\u5982\u679c\u4f60\u9700\u8981\u7684\u8bdd\uff0c\u6240\u6709\u8fd9\u4e9b\u683c\u5f0f\u90fd\u652f\u6301\u5b57\u7b26\u4e32\u63d2\u503c\u3002<\/p>\n<pre class=\"post-pre\"><code>droplets <span class=\"token operator\">=<\/span> <span class=\"token number\">5<\/span><br>print <span class=\"token string-literal\"><span class=\"token string\">%{Sammy says, \"I just created <\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">droplets<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\"> droplets!\"}<\/span><\/span><\/code><\/pre>\n<p>\u5728Ruby\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u8fd8\u4f1a\u89c1\u5230<code>%Q{}<\/code>\u548c<code>%q{}<\/code>\u7528\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\u3002<code>%Q{}<\/code>\u8bed\u6cd5\u4e0e\u53cc\u5f15\u53f7\u5b57\u7b26\u4e32\u7684\u7528\u6cd5\u5b8c\u5168\u76f8\u540c\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u4e0d\u5fc5\u8f6c\u4e49\u53cc\u5f15\u53f7\uff0c\u540c\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u503c\u3002<\/p>\n<pre class=\"post-pre\"><code>droplets <span class=\"token operator\">=<\/span> <span class=\"token number\">5<\/span><br>print <span class=\"token string-literal\"><span class=\"token string\">%Q{Sammy says, \"I just created <\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">droplets<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\"> droplets!\"}<\/span><\/span><\/code><\/pre>\n<p><code>%q{}<\/code>\u8bed\u6cd5\u4e0e\u5355\u5f15\u53f7\u5b57\u7b26\u4e32\u4e00\u6a21\u4e00\u6837\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">%q{Sammy says, \"I'm a happy shark!\"}<\/span><\/span><\/code><\/pre>\n<p>\u5728\u67d0\u4e9b\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u770b\u5230\u4f7f\u7528\u62ec\u53f7\u6216\u65b9\u62ec\u53f7\u7684<code>%q<\/code>\u548c<code>%Q<\/code>\u8bed\u6cd5\uff0c\u800c\u4e0d\u662f\u82b1\u62ec\u53f7\u3002<\/p>\n<p>\u6b63\u5982\u4f60\u6240\u770b\u5230\u7684\uff0c\u5728Ruby\u4e2d\u521b\u5efa\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\u3002\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u4e00\u79cd\u65b9\u6cd5\uff0c\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u8981\u4fdd\u6301\u4e00\u81f4\u3002\u4f60\u4f1a\u53d1\u73b0<code>%Q{}<\/code>\u548c<code>%{}<\/code>\u65b9\u6cd5\u6700\u5e38\u89c1\u3002<\/p>\n<p>\u65e2\u7136\u4f60\u5df2\u7ecf\u77e5\u9053\u5982\u4f55\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u4e86\uff0c\u90a3\u6211\u4eec\u6765\u770b\u770b\u5982\u4f55\u5904\u7406\u957f\u5b57\u7b26\u4e32\u548c\u6362\u884c\u5b57\u7b26\u3002<\/p>\n<h2>\u957f\u5b57\u7b26\u4e32\u548c\u6362\u884c<\/h2>\n<p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u60f3\u5728\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u6362\u884c\u7b26\u6216\u56de\u8f66\u7b26\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>\"\\n\"<\/code>\u6216<code>\"\\r\"<\/code>\u8f6c\u4e49\u5b57\u7b26\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u6362\u884c\u3002<\/p>\n<pre class=\"post-pre\"><code>output <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"This is\\na string\\nwith newlines\"<\/span><\/span><br>puts output<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u4f1a\u4ea7\u751f\u8fd9\u4e2a\u8f93\u51fa\u3002<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">OutputThis is<br>a string<br>with newlines<\/div><\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5728\u6280\u672f\u4e0a\u53ef\u4ee5\u5b9e\u73b0\u6211\u4eec\u5728\u591a\u884c\u8f93\u51fa\u3002\u7136\u800c\uff0c\u5c06\u4e00\u4e2a\u975e\u5e38\u957f\u7684\u5b57\u7b26\u4e32\u5199\u5728\u4e00\u884c\u4e0a\u4f1a\u4f7f\u5176\u5f88\u96be\u9605\u8bfb\u548c\u5904\u7406\u3002\u6709\u51e0\u79cd\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u9996\u5148\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u8fd0\u7b97\u7b26\u5c06\u5b57\u7b26\u4e32\u5206\u6210\u591a\u884c\u3002<\/p>\n<p>\u8fd9\u662f\u6587\u7ae0\u300a\u5982\u4f55\u5728Ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u300b\u7684\u7b2c5\u90e8\u5206\uff08\u51715\u90e8\u5206\uff09\u3002<\/p>\n<p>\u5185\u5bb9\u7247\u6bb5:<\/p>\n<pre class=\"post-pre\"><code>output <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"This is a\\n\"<\/span><\/span> <span class=\"token operator\">+<\/span>\r\n<span class=\"token string-literal\"><span class=\"token string\">\"longer string\\n\"<\/span><\/span> <span class=\"token operator\">+<\/span>\r\n<span class=\"token string-literal\"><span class=\"token string\">\"with newlines.\"<\/span><\/span>\r\nputs output\r\n<\/code><\/pre>\n<p>\u8fd9\u53ea\u662f\u5c06\u4e09\u4e2a\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u7c7b\u4f3c\u4e8e\u4f60\u4e4b\u524d\u5df2\u7ecf\u505a\u8fc7\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u60a8\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u52a0\u5165\u6362\u884c\u7b26\uff1a<\/p>\n<pre class=\"post-pre\"><code>output <span class=\"token operator\">=<\/span> \"This is a\r\nlonger string\r\nwith newlines\"\r\nputs output\r\n<\/code><\/pre>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u5176\u4ed6\u7684\u5b57\u7b26\u4e32\u8bed\u6cd5\u6765\u521b\u5efa\u591a\u884c\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"post-pre\"><code>output <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">%{This is a\r\nlonger string\r\nwith newlines}<\/span><\/span>\r\nputs output\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8bf7\u6ce8\u610f\u6211\u4eec\u4e0d\u9700\u8981\u4f7f\u7528\u6362\u884c\u7b26\uff08<code>\\n<\/code>\uff09\u5b57\u7b26\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u4fdd\u7559\u7a7a\u683c\uff0c\u5305\u62ec\u7f29\u8fdb\u548c\u6362\u884c\u7b26\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u8f93\u51fa\u5c06\u5305\u542b\u6362\u884c\u7b26\u4ee5\u53ca\u6240\u6709\u524d\u5bfc\u7684\u7f29\u8fdb\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>This is a\r\nlonger string\r\nwith newlines\r\n<\/code><\/pre>\n<p>\u4e3a\u4e86\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\u53d1\u751f\uff0c\u8bf7\u4ece\u4f60\u7684\u4ee3\u7801\u4e2d\u53bb\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3002<\/p>\n<pre class=\"post-pre\"><code>output <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">%{This is a\r\nlonger string\r\nwith newlines\r\n}<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528heredoc\uff08\u5728\u7a0b\u5e8f\u4e2d\u7528\u4e8e\u591a\u884c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u672f\u8bed\uff09\u6765\u521b\u5efa\u591a\u884c\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u7f16\u5199\u6b64\u4ee3\u7801\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"post-pre\"><code>output <span class=\"token operator\">=<\/span> <span class=\"token string-literal heredoc-string\"><span class=\"token delimiter\"><span class=\"token punctuation\">&lt;&lt;-<\/span><span class=\"token symbol\">END<\/span><\/span><span class=\"token string\">\r\nThis is a\r\nlonger string\r\nwith newlines\r\n<\/span><span class=\"token delimiter\"><span class=\"token symbol\">END<\/span><\/span><\/span>\r\n<\/code><\/pre>\n<p><code>&lt;&lt;-END<\/code> \u548c <code>END<\/code> \u6807\u8bb0\u8868\u793aheredoc\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u3002<\/p>\n<p>Ruby\u7684Heredocs\u4e5f\u53ef\u4ee5\u4fdd\u7559\u7a7a\u683c\u5b57\u7b26\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60\u5728Heredoc\u4e2d\u7f29\u8fdb\u4ee3\u7801\uff0c\u524d\u5bfc\u7f29\u8fdb\u4e5f\u4f1a\u88ab\u4fdd\u7559\u3002\u56e0\u6b64\uff0c\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"post-pre\"><code>output <span class=\"token operator\">=<\/span> <span class=\"token string-literal heredoc-string\"><span class=\"token delimiter\"><span class=\"token punctuation\">&lt;&lt;-<\/span><span class=\"token symbol\">END<\/span><\/span><span class=\"token string\">\r\n  This is a\r\n  longer string\r\n  with newlines\r\n<\/span><span class=\"token delimiter\"><span class=\"token symbol\">END<\/span><\/span><\/span>\r\n<\/code><\/pre>\n<p>\u5c06\u4ee5\u4e24\u4e2a\u7a7a\u683c\u7f29\u8fdb\u7684\u65b9\u5f0f\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p>Ruby 2.3\u53ca\u66f4\u9ad8\u7248\u672c\u63d0\u4f9b\u4e86\u201c\u6ce2\u6d6a\u957f\u5b57\u7b26\u4e32\u201d\u8bed\u6cd5\uff0c\u53ef\u4ee5\u81ea\u52a8\u5220\u9664\u524d\u5bfc\u7a7a\u767d\u3002\u5c06\u6ce2\u6d6a\u7ebf\u201c-\u201d\u66ff\u6362\u4e3a\u6ce2\u6d6a\u53f7\u201c~\u201d\uff0c\u6240\u4ee5<code>&lt;&lt;-<\/code>\u53d8\u4e3a<code>&lt;&lt;~<\/code>\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"post-pre\"><code>output <span class=\"token operator\">=<\/span> <span class=\"token string-literal heredoc-string\"><span class=\"token delimiter\"><span class=\"token punctuation\">&lt;&lt;~<\/span><span class=\"token symbol\">END<\/span><\/span><span class=\"token string\">\r\n  This is a\r\n  longer string\r\n  with newlines\r\n  and the code is indented\r\n  but the output is not.\r\n<\/span><span class=\"token delimiter\"><span class=\"token symbol\">END<\/span><\/span><\/span>\r\n<\/code><\/pre>\n<p>\u8fd9\u4f1a\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>This is a\r\nlonger string\r\nwith newlines\r\nand the code is indented\r\nbut the output is not.\r\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u8ba9\u4f60\u4f7f\u7528\u7eaf\u6587\u672c\u5185\u5bb9\u5e76\u4fdd\u6301\u4ee3\u7801\u6574\u9f50\u7f29\u8fdb\u3002<\/p>\n<p>\u5728Ruby\u4e2d\uff0cHeredocs\u4e5f\u652f\u6301\u5b57\u7b26\u4e32\u63d2\u503c\u3002<\/p>\n<p>\u6b63\u5982\u4f60\u6240\u89c1\uff0cRuby\u4e2d\u6709\u5f88\u591a\u5904\u7406\u6362\u884c\u548c\u591a\u884c\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u5728\u4e0e\u73b0\u6709\u7684Ruby\u4ee3\u7801\u4e00\u8d77\u5de5\u4f5c\u65f6\uff0c\u4f60\u4f1a\u9047\u5230\u6240\u6709\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u9879\u76ee\u90fd\u503e\u5411\u4e8e\u6709\u81ea\u5df1\u7684\u98ce\u683c\u3002\u5728\u4f60\u81ea\u5df1\u7684\u4ee3\u7801\u4e2d\uff0c\u9009\u62e9\u9002\u5408\u4f60\u7684\u98ce\u683c\u5e76\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<h2>\u5b57\u7b26\u4e32\u590d\u5236<\/h2>\n<p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528 Ruby \u6765\u591a\u6b21\u91cd\u590d\u4e00\u4e2a\u5b57\u7b26\u5e8f\u5217\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>*<\/code> \u64cd\u4f5c\u7b26\u6765\u5b9e\u73b0\u3002\u548c <code>+<\/code> \u64cd\u4f5c\u7b26\u4e00\u6837\uff0c<code>*<\/code> \u64cd\u4f5c\u7b26\u5728\u548c\u6570\u5b57\u4e00\u8d77\u4f7f\u7528\u65f6\u6709\u4e0d\u540c\u7684\u7528\u9014\uff0c\u5b83\u662f\u4e58\u6cd5\u8fd0\u7b97\u7b26\u3002\u5f53\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u6574\u6570\u4e00\u8d77\u4f7f\u7528\u65f6\uff0c<code>*<\/code> \u662f\u5b57\u7b26\u4e32\u590d\u5236\u8fd0\u7b97\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u63d0\u4f9b\u7684\u6574\u6570\u6765\u91cd\u590d\u4e00\u4e2a\u5b57\u7b26\u4e32\u591a\u5c11\u6b21\u3002<\/p>\n<p>\u8981\u6253\u5370\u4e5d\u6b21\u201cSammy\u201d\uff0c\u60a8\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"post-pre\"><code>print <span class=\"token string-literal\"><span class=\"token string\">\"Sammy\"<\/span><\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">9<\/span>\r\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>SammySammySammySammySammySammySammySammySammy\r\n<\/code><\/pre>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u6765\u521b\u5efa\u4e00\u4e9b\u6f02\u4eae\u7684ASCII\u827a\u672f\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>banner.rb<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u3002<\/p>\n<pre class=\"post-pre\"><code>puts <span class=\"token string-literal\"><span class=\"token string\">\"=\"<\/span><\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">15<\/span>\r\nputs <span class=\"token string-literal\"><span class=\"token string\">\"| Hello World |\"<\/span><\/span>\r\nputs <span class=\"token string-literal\"><span class=\"token string\">\"=\"<\/span><\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">15<\/span>\r\n<\/code><\/pre>\n<p>\u4f60\u5728\u8fd0\u884c\u7a0b\u5e8f\u4e4b\u524d\u80fd\u60f3\u8c61\u5b83\u4f1a\u4ea7\u751f\u4ec0\u4e48\u5417\uff1f<\/p>\n<p>\u5b83\u4ea7\u751f\u4e86\u4ee5\u4e0b\u8f93\u51fa\u3002<\/p>\n<pre class=\"post-pre\"><code><div class=\"secondary-code-label\" title=\"Output\">\u8f93\u51fa<\/div>===============\r\n| Hello World |\r\n===============\r\n<\/code><\/pre>\n<p>\u8fd9\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u8ba9\u8ba1\u7b97\u673a\u4e3a\u60a8\u6267\u884c\u91cd\u590d\u4efb\u52a1\u3002<\/p>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u5728\u8fd9\u4e2a\u6559\u7a0b\u4e2d\uff0c\u4f60\u5b66\u4e60\u4e86\u5982\u4f55\u5728Ruby\u7f16\u7a0b\u8bed\u8a00\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u6570\u636e\u7c7b\u578b\u3002\u4f60\u521b\u5efa\u4e86\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u5c06\u5176\u4e0e\u5176\u4ed6\u5b57\u7b26\u4e32\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u5904\u7406\u4e86\u6362\u884c\u7b26\u3001\u5f15\u53f7\u548c\u6487\u53f7\u3002\u7136\u540e\uff0c\u4f60\u4f7f\u7528\u4e86\u5b57\u7b26\u4e32\u63d2\u503c\u6765\u66f4\u8f7b\u677e\u5730\u6df7\u5408\u5b57\u7b26\u4e32\u548c\u53d8\u91cf\u503c\uff0c\u5e76\u5b66\u4e60\u4e86\u5982\u4f55\u91cd\u590d\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u5b57\u7b26\u4e32\u662f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\u7ec4\u6210\u7684\u5e8f\u5217\uff0c\u8fd9\u4e9b\u5b57\u7b26\u53ef\u4ee5\u662f\u5b57\u6bcd\u3001\u6570\u5b57\u6216\u7b26\u53f7\u3002 \u5728Ruby\u4e2d\uff0c\u5b57\u7b26\u4e32\u662f\u5bf9\u8c61\u3002\u4e0e\u8bb8\u591a [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[258,243,260,206,259],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ruby","tag-243","tag-260","tag-206","tag-259"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790 - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"\u6df1\u5165\u5b66\u4e60Ruby\u4e2d\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u64cd\u4f5c\u548c\u5904\u7406\u6280\u5de7\uff0c\u5305\u62ec\u521b\u5efa\u3001\u4fee\u6539\u3001\u67e5\u627e\u3001\u66ff\u6362\u3001\u683c\u5f0f\u5316\u7b49\u3002\u672c\u6559\u7a0b\u63d0\u4f9b\u4e30\u5bcc\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u52a9\u60a8\u8f7b\u677e\u638c\u63e1Ruby\u5b57\u7b26\u4e32\u5904\u7406\u7684\u7cbe\u9ad3\uff0c\u63d0\u5347\u7f16\u7a0b\u6548\u7387\u3002\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790\" \/>\n<meta property=\"og:description\" content=\"\u6df1\u5165\u5b66\u4e60Ruby\u4e2d\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u64cd\u4f5c\u548c\u5904\u7406\u6280\u5de7\uff0c\u5305\u62ec\u521b\u5efa\u3001\u4fee\u6539\u3001\u67e5\u627e\u3001\u66ff\u6362\u3001\u683c\u5f0f\u5316\u7b49\u3002\u672c\u6559\u7a0b\u63d0\u4f9b\u4e30\u5bcc\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u52a9\u60a8\u8f7b\u677e\u638c\u63e1Ruby\u5b57\u7b26\u4e32\u5904\u7406\u7684\u7cbe\u9ad3\uff0c\u63d0\u5347\u7f16\u7a0b\u6548\u7387\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-10T00:08:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-31T12:57:41+00:00\" \/>\n<meta name=\"author\" content=\"\u79d1, \u9896\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u79d1, \u9896\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/\",\"name\":\"Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790 - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\"},\"datePublished\":\"2023-07-10T00:08:31+00:00\",\"dateModified\":\"2025-07-31T12:57:41+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/8ca01ba7f7362ad4edb7da206a12f29e\"},\"description\":\"\u6df1\u5165\u5b66\u4e60Ruby\u4e2d\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u64cd\u4f5c\u548c\u5904\u7406\u6280\u5de7\uff0c\u5305\u62ec\u521b\u5efa\u3001\u4fee\u6539\u3001\u67e5\u627e\u3001\u66ff\u6362\u3001\u683c\u5f0f\u5316\u7b49\u3002\u672c\u6559\u7a0b\u63d0\u4f9b\u4e30\u5bcc\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u52a9\u60a8\u8f7b\u677e\u638c\u63e1Ruby\u5b57\u7b26\u4e32\u5904\u7406\u7684\u7cbe\u9ad3\uff0c\u63d0\u5347\u7f16\u7a0b\u6548\u7387\u3002\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/zh\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/\",\"name\":\"Blog - Silicon Cloud\",\"description\":\"\",\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/8ca01ba7f7362ad4edb7da206a12f29e\",\"name\":\"\u79d1, \u9896\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8a6fb3cc7ba2f69d2189ba532aec4633ea7ed75ac0af162ec367cb3abc0fb2af?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8a6fb3cc7ba2f69d2189ba532aec4633ea7ed75ac0af162ec367cb3abc0fb2af?s=96&d=mm&r=g\",\"caption\":\"\u79d1, \u9896\"},\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/author\/keying\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790 - Blog - Silicon Cloud","description":"\u6df1\u5165\u5b66\u4e60Ruby\u4e2d\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u64cd\u4f5c\u548c\u5904\u7406\u6280\u5de7\uff0c\u5305\u62ec\u521b\u5efa\u3001\u4fee\u6539\u3001\u67e5\u627e\u3001\u66ff\u6362\u3001\u683c\u5f0f\u5316\u7b49\u3002\u672c\u6559\u7a0b\u63d0\u4f9b\u4e30\u5bcc\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u52a9\u60a8\u8f7b\u677e\u638c\u63e1Ruby\u5b57\u7b26\u4e32\u5904\u7406\u7684\u7cbe\u9ad3\uff0c\u63d0\u5347\u7f16\u7a0b\u6548\u7387\u3002","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\/","og_locale":"zh_CN","og_type":"article","og_title":"Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790","og_description":"\u6df1\u5165\u5b66\u4e60Ruby\u4e2d\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u64cd\u4f5c\u548c\u5904\u7406\u6280\u5de7\uff0c\u5305\u62ec\u521b\u5efa\u3001\u4fee\u6539\u3001\u67e5\u627e\u3001\u66ff\u6362\u3001\u683c\u5f0f\u5316\u7b49\u3002\u672c\u6559\u7a0b\u63d0\u4f9b\u4e30\u5bcc\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u52a9\u60a8\u8f7b\u677e\u638c\u63e1Ruby\u5b57\u7b26\u4e32\u5904\u7406\u7684\u7cbe\u9ad3\uff0c\u63d0\u5347\u7f16\u7a0b\u6548\u7387\u3002","og_url":"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2023-07-10T00:08:31+00:00","article_modified_time":"2025-07-31T12:57:41+00:00","author":"\u79d1, \u9896","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u79d1, \u9896","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/","url":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/","name":"Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790 - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website"},"datePublished":"2023-07-10T00:08:31+00:00","dateModified":"2025-07-31T12:57:41+00:00","author":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/8ca01ba7f7362ad4edb7da206a12f29e"},"description":"\u6df1\u5165\u5b66\u4e60Ruby\u4e2d\u5b57\u7b26\u4e32\u7684\u5404\u79cd\u64cd\u4f5c\u548c\u5904\u7406\u6280\u5de7\uff0c\u5305\u62ec\u521b\u5efa\u3001\u4fee\u6539\u3001\u67e5\u627e\u3001\u66ff\u6362\u3001\u683c\u5f0f\u5316\u7b49\u3002\u672c\u6559\u7a0b\u63d0\u4f9b\u4e30\u5bcc\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u52a9\u60a8\u8f7b\u677e\u638c\u63e1Ruby\u5b57\u7b26\u4e32\u5904\u7406\u7684\u7cbe\u9ad3\uff0c\u63d0\u5347\u7f16\u7a0b\u6548\u7387\u3002","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/zh\/blog\/"},{"@type":"ListItem","position":2,"name":"Ruby\u5b57\u7b26\u4e32\u5904\u7406\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u6280\u5de7\u5168\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website","url":"https:\/\/www.silicloud.com\/zh\/blog\/","name":"Blog - Silicon Cloud","description":"","inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/8ca01ba7f7362ad4edb7da206a12f29e","name":"\u79d1, \u9896","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8a6fb3cc7ba2f69d2189ba532aec4633ea7ed75ac0af162ec367cb3abc0fb2af?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8a6fb3cc7ba2f69d2189ba532aec4633ea7ed75ac0af162ec367cb3abc0fb2af?s=96&d=mm&r=g","caption":"\u79d1, \u9896"},"url":"https:\/\/www.silicloud.com\/zh\/blog\/author\/keying\/"},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e5%a4%84%e7%90%86%e5%ad%97%e7%ac%a6%e4%b8%b2\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/comments?post=21"}],"version-history":[{"count":4,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":109469,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/21\/revisions\/109469"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/media?parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/categories?post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/tags?post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}