{"id":585,"date":"2023-06-05T22:33:25","date_gmt":"2023-12-02T16:32:00","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%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/"},"modified":"2025-08-02T20:24:00","modified_gmt":"2025-08-02T12:24:00","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/","title":{"rendered":"Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a"},"content":{"rendered":"<h3>\u5f15\u8a00\u5728\u60a8\u521b\u5efa\u7684\u6bcf\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u5c06\u5305\u542b\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u9700\u8981\u7262\u8bb0\u7684\u662f\uff0c\u901a\u5e38\u60a8\u5c06\u5728\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e2d\u6267\u884c\u64cd\u4f5c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u60a8\u5c06\u5bf9\u6570\u5b57\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff0c\u6216\u5c06\u5b57\u7b26\u4e32\u8fde\u63a5\u5728\u4e00\u8d77\u3002<\/p>\n<p>\u6709\u65f6\u6570\u636e\u6765\u81ea\u5916\u90e8\u6765\u6e90\uff0c\u5982\u952e\u76d8\u3001API\u54cd\u5e94\u6216\u6570\u636e\u5e93\uff0c\u4f60\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4ee5\u4fbf\u4f7f\u7528\u3002Ruby\u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u4f60\u5c06\u628a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u5e76\u5728\u5b57\u7b26\u4e32\u548c\u7b26\u53f7\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<h2>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57Ruby\u63d0\u4f9b\u4e86`to_i`\u548c`to_f`\u65b9\u6cd5\uff0c\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u3002`to_i`\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c`to_f`\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"5\"<\/span><\/span><span class=\"token punctuation\">.<\/span>to_i       <span class=\"token comment\"># 5<\/span>\r\n<span class=\"token string-literal\"><span class=\"token string\">\"55.5\"<\/span><\/span><span class=\"token punctuation\">.<\/span>to_i    <span class=\"token comment\"># 55<\/span>\r\n<span class=\"token string-literal\"><span class=\"token string\">\"55.5\"<\/span><\/span><span class=\"token punctuation\">.<\/span>to_f    <span class=\"token comment\"># 55.5<\/span>\r\n<\/code><\/pre>\n<p>\u4e3a\u4e86\u8bc1\u660e\u8fd9\u4e00\u70b9\uff0c\u521b\u5efa\u4e00\u4e2a\u5c0f\u7a0b\u5e8f\uff0c\u63d0\u793a\u8f93\u5165\u4e24\u4e2a\u6570\u5b57\u5e76\u663e\u793a\u5b83\u4eec\u7684\u548c\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aadder.rb\u7684\u65b0\u7684Ruby\u7a0b\u5e8f\uff0c\u5176\u4e2d\u5305\u542b\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<div>\u52a0\u6cd5\u5668.rb<\/p>\n<pre class=\"post-pre\"><code>print <span class=\"token string-literal\"><span class=\"token string\">\"What is the first number? \"<\/span><\/span>\r\nfirst_number <span class=\"token operator\">=<\/span> gets<span class=\"token punctuation\">.<\/span>chop\r\n\r\nprint <span class=\"token string-literal\"><span class=\"token string\">\"What is the second number? \"<\/span><\/span>\r\nsecond_number <span class=\"token operator\">=<\/span> gets<span class=\"token punctuation\">.<\/span>chop\r\n\r\nsum <span class=\"token operator\">=<\/span> first_number <span class=\"token operator\">+<\/span> second_number\r\n\r\nprint sum\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7a0b\u5e8f\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u610f\u60f3\u4e0d\u5230\u7684\u7b54\u6848\u3002<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\">ruby adder.rb<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"Output\">Output<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>What is the first number? <mark>5<\/mark> What is the second number? <mark>5<\/mark> <mark>55<\/mark><\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u663e\u793a5\u548c5\u7684\u548c\u662f55\u3002\u4f60\u77e5\u9053\u8fd9\u662f\u4e0d\u5bf9\u7684\uff0c\u4f46\u4ece\u6280\u672f\u4e0a\u6765\u8bf4\uff0c\u8ba1\u7b97\u673a\u5e76\u6ca1\u6709\u9519\u3002\u7a0b\u5e8f\u63d0\u793a\u4f60\u8f93\u5165\u4e24\u4e2a\u6570\u5b57\uff0c\u4f46\u4f60\u5728\u952e\u76d8\u4e0a\u8f93\u5165\u4e86\u5b83\u4eec\u3002\u4f60\u6ca1\u6709\u53d1\u9001\u6570\u5b575\uff0c\u800c\u662f\u53d1\u9001\u5b57\u7b26&#8221;5&#8243;\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f60\u7684\u7a0b\u5e8f\u5c06\u4f60\u7684\u8f93\u5165\u90fd\u89c6\u4e3a\u5b57\u7b26\u4e32\uff0c\u5f53\u4f60\u5c06\u5b57\u7b26\u4e32&#8221;5&#8243;\u548c&#8221;5&#8243;\u76f8\u52a0\u65f6\uff0c\u5f97\u5230\u7684\u662f\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32&#8221;55&#8243;\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u5c06\u8fd9\u4e24\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u3002\u901a\u8fc7\u4f7f\u7528to_f\u65b9\u6cd5\uff0c\u4fee\u6539\u4f60\u7684\u7a0b\u5e8f\uff0c\u4f7f\u5176\u5c06\u4e24\u4e2a\u6570\u5b57\u90fd\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/p>\n<div>\u52a0\u6cd5\u5668.rb<\/p>\n<pre class=\"post-pre\"><code>print <span class=\"token string-literal\"><span class=\"token string\">\"What is the first number? \"<\/span><\/span>\r\nfirst_number <span class=\"token operator\">=<\/span> gets<span class=\"token punctuation\">.<\/span>chop\r\n\r\nprint <span class=\"token string-literal\"><span class=\"token string\">\"What is the second number? \"<\/span><\/span>\r\nsecond_number <span class=\"token operator\">=<\/span> gets<span class=\"token punctuation\">.<\/span>chop\r\n\r\n<span class=\"token comment\"># convert strings to numbers<\/span>\r\n<mark>first_number <span class=\"token operator\">=<\/span> first_number<span class=\"token punctuation\">.<\/span>to_f<\/mark>\r\n<mark>second_number <span class=\"token operator\">=<\/span> second_number<span class=\"token punctuation\">.<\/span>to_f<\/mark>\r\n\r\nsum <span class=\"token operator\">=<\/span> first_number <span class=\"token operator\">+<\/span> second_number\r\n\r\nprint sum\r\n<\/code><\/pre>\n<p>\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<ol>\n<li data-prefix=\"$\">ruby adder.rb<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>\u8fd9\u4e00\u6b21\uff0c\u7ed3\u679c\u5c06\u4f1a\u4e0d\u540c\u3002<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"Output\">Output<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>What is the first number? <mark>5<\/mark> What is the second number? <mark>5<\/mark> <mark>10.0<\/mark><\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>\u5f53\u4f60\u8f93\u51655\u548c\u518d\u6b21\u8f93\u51655\u65f6\uff0c\u4f60\u5c06\u5f97\u523010.0\u3002<\/p>\n<p>\u5f53\u5b57\u7b26\u4e32\u4e0d\u662f\u6570\u5b57\u65f6\uff0cto_i\u548cto_f\u65b9\u6cd5\u4f1a\u6709\u4e00\u4e9b\u6709\u8da3\u7684\u884c\u4e3a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"123-abc\"<\/span><\/span><span class=\"token punctuation\">.<\/span>to_i\r\n<\/code><\/pre>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"Output\">Output<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>123<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5c06\u5b57\u7b26\u4e32&#8221;123-abc&#8221;\u8f6c\u6362\u4e3a\u6574\u6570\u5c06\u5f97\u5230\u6574\u6570123\u3002to_i\u65b9\u6cd5\u5728\u9047\u5230\u7b2c\u4e00\u4e2a\u975e\u6570\u5b57\u5b57\u7b26\u65f6\u505c\u6b62\u3002Ruby\u7f51\u7edc\u5f00\u53d1\u8005\u901a\u8fc7\u521b\u5efa\u50cf15-sammy-shark\u8fd9\u6837\u7684URL\u6765\u5229\u7528\u8fd9\u4e00\u70b9\uff0c\u5176\u4e2d15\u662f\u7528\u4e8e\u67e5\u627e\u8bb0\u5f55\u7684\u5185\u90e8ID\uff0c\u800csammy-shark\u5728URL\u4e2d\u63d0\u4f9b\u4e86\u6587\u672c\u63cf\u8ff0\u3002\u5f53Ruby\u5c0615-sammy-shark\u8f6c\u6362\u4e3a\u6574\u6570\u65f6\uff0c\u7ed3\u679c\u662f15\uff0c\u800c-sammy-shark\u90e8\u5206\u88ab\u622a\u65ad\u548c\u4e22\u5f03\u3002\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u6574\u6570\u4ece\u6570\u636e\u5e93\u4e2d\u68c0\u7d22\u8bb0\u5f55\u3002<\/p>\n<p>\u8fd9\u662f\u53e6\u4e00\u4e2a\u6574\u6570\u884c\u4e3a\u7684\u4f8b\u5b50\uff0c\u5b83\u53ef\u80fd\u4f1a\u8ba9\u4f60\u63aa\u624b\u4e0d\u53ca:<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"abc\"<\/span><\/span><span class=\"token punctuation\">.<\/span>to_i\r\n<\/code><\/pre>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"Output\">Output<\/p>\n<p>\u8fd9\u662f\u6587\u7ae0\u300a\u5982\u4f55\u5728Ruby\u4e2d\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u300b\u7684\u7b2c2\u90e8\u5206\uff08\u51714\u90e8\u5206\uff09\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u65e0\u6cd5\u8f6c\u6362\uff0c<code>to_i<\/code>\u65b9\u6cd5\u8fd4\u56de0\u3002\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4e0d\u5e0c\u671b\u7684\u884c\u4e3a\uff1b\u5982\u679c\u7528\u6237\u5728\u60a8\u7684\u7a0b\u5e8f\u4e2d\u8f93\u5165&#8221;abc&#8221;\uff0c\u5e76\u4e14\u60a8\u5c06\u8be5\u503c\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u5c06\u67d0\u4e2a\u6570\u503c\u9664\u4ee5\u8be5\u503c\uff0c\u60a8\u7684\u7a0b\u5e8f\u5c06\u5d29\u6e83\uff0c\u56e0\u4e3a\u5b83\u65e0\u6cd5\u9664\u4ee5\u96f6\u3002<\/p>\n<p>Ruby\u63d0\u4f9b\u53e6\u4e00\u79cd\u6267\u884c\u6b64\u8f6c\u6362\u7684\u65b9\u5f0f\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>Integer<\/code>\u548c<code>Float<\/code>\u7684\u65b9\u6cd5\u6765\u8f6c\u6362\u6570\u636e\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token builtin\">Integer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal\"><span class=\"token string\">\"123\"<\/span><\/span><span class=\"token punctuation\">)<\/span>\r\n<\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa<\/div>\n<pre class=\"post-pre\"><code>123\r\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u7ed9<code>Integer<\/code>\u65b9\u6cd5\u4f20\u5165\u4e00\u4e2a\u65e0\u6cd5\u8f6c\u6362\u7684\u503c\uff0cRuby\u4f1a\u5f15\u53d1\u4e00\u4e2a\u9519\u8bef\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token builtin\">Integer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string-literal\"><span class=\"token string\">\"123abc\"<\/span><\/span><span class=\"token punctuation\">)<\/span>\r\n<\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa<\/div>\n<pre class=\"post-pre\"><code>ArgumentError: invalid value for Integer(): \"123abc\"\r\n<\/code><\/pre>\n<p>\u60a8\u53ef\u4ee5\u5904\u7406\u9519\u8bef\uff0c\u5e76\u5411\u7528\u6237\u63d0\u4f9b\u4e00\u6761\u4fe1\u606f\uff0c\u8981\u6c42\u4ed6\u4eec\u63d0\u4f9b\u66f4\u597d\u7684\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u592a\u65b9\u4fbf\uff0c\u4f46\u53ef\u4ee5\u4f7f\u6570\u636e\u5b8c\u6574\u6027\u66f4\u597d\uff0c\u56e0\u4e3a\u60a8\u7684\u6570\u636e\u4e0d\u4f1a\u88ab\u5f3a\u5236\u6539\u53d8\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u60a8\u5c06\u5b66\u4e60\u5982\u4f55\u5c06\u5176\u4ed6\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<h2>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32<\/h2>\n<p>Ruby\u63d0\u4f9b\u4e86<code>to_s<\/code>\u65b9\u6cd5\uff0c\u5c06\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token number\">25.<\/span>to_s                    <span class=\"token comment\"># \"25\"<\/span>\r\n<span class=\"token punctuation\">(<\/span><span class=\"token number\">25.5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>to_s                <span class=\"token comment\"># \"25.5\"<\/span>\r\n<span class=\"token punctuation\">[<\/span><span class=\"token string-literal\"><span class=\"token string\">\"Sammy\"<\/span><\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"Shark\"<\/span><\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">.<\/span>to_s    <span class=\"token comment\"># \"[\\\"Sammy\\\", \\\"Shark\\\"]\"<\/span>\r\n<\/code><\/pre>\n<p>\u5728\u521b\u5efa\u7a0b\u5e8f\u8f93\u51fa\u65f6\uff0c\u60a8\u7ecf\u5e38\u9700\u8981\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u5047\u8bbe\u4f60\u60f3\u5728\u953b\u70bc\u540e\u8ffd\u8e2a\u4e00\u4e2a\u4eba\u6bcf\u5929\u71c3\u70e7\u7684\u5361\u8def\u91cc\u3002\u4f60\u5e0c\u671b\u5c06\u8fd9\u4e2a\u8fdb\u5c55\u5c55\u793a\u7ed9\u7528\u6237\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u9700\u8981\u540c\u65f6\u8f93\u51fa\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u503c\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>calories.rb<\/code>\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<div class=\"code-file-label\">\u5361\u8def\u91cc.rb<\/div>\n<pre class=\"post-pre\"><code>user <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"Sammy\"<\/span><\/span>\r\ncalories <span class=\"token operator\">=<\/span> <span class=\"token number\">100<\/span>\r\n\r\nprint <span class=\"token string-literal\"><span class=\"token string\">\"\u606d\u559c\u60a8\uff0c\"<\/span><\/span> <span class=\"token operator\">+<\/span> user <span class=\"token operator\">+<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"\uff01\u60a8\u521a\u521a\u5728\u8fd9\u6b21\u953b\u70bc\u4e2d\u71c3\u70e7\u4e86 \"<\/span><\/span> <span class=\"token operator\">+<\/span> calories <span class=\"token operator\">+<\/span> <span class=\"token string-literal\"><span class=\"token string\">\" \u5361\u8def\u91cc\u3002\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u6b63\u5728\u786c\u7f16\u7801\u540d\u79f0\u548c\u70ed\u91cf\uff0c\u4f46\u5728\u771f\u5b9e\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u4f1a\u4ece\u53e6\u4e00\u4e2a\u6765\u6e90\uff08\u6bd4\u5982\u6570\u636e\u5e93\u6216API\uff09\u4e2d\u83b7\u53d6\u8fd9\u4e9b\u503c\u3002<\/p>\n<p>\u8fd0\u884c\u4ee5\u4e0b\u7a0b\u5e8f\uff1a<\/p>\n<ol>\n<li data-prefix=\"$\">ruby calories.rb<\/li>\n<\/ol>\n<p>\u5f53\u4f60\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u4f60\u4f1a\u9047\u5230\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa<\/div>\n<pre class=\"post-pre\"><code>... TypeError: no implicit conversion of Integer into String (TypeError)\r\n<\/code><\/pre>\n<p>Ruby\u4e0d\u5141\u8bb8\u60a8\u5c06\u5361\u8def\u91cc\u53d8\u91cf\u6dfb\u52a0\u5230\u5176\u4f59\u7684\u8f93\u51fa\u4e2d\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u4e2a\u6574\u6570\u3002\u60a8\u4e0d\u80fd\u901a\u8fc7\u5728\u5176\u5468\u56f4\u52a0\u5f15\u53f7\u6765\u5c06\u5176\u66f4\u6539\u4e3a\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u5361\u8def\u91cc\u6570\u636e\u53ef\u80fd\u6765\u81ea\u60a8\u65e0\u6cd5\u63a7\u5236\u7684\u5730\u65b9\u3002\u76f8\u53cd\uff0c\u60a8\u9700\u8981\u5c06\u5361\u8def\u91cc\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4ee5\u4fbf\u5c06\u5176\u4e0e\u5176\u4f59\u7684\u8f93\u51fa\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n<p>\u4f7f\u7528<code>to_s<\/code>\u65b9\u6cd5\u5c06\u8f93\u51fa\u884c\u4fee\u6539\u4e3a\u5c06\u5361\u8def\u91cc\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<div class=\"code-file-label\">\u5361\u8def\u91cc.rb<\/div>\n<pre class=\"post-pre\"><code>user <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"Sammy\"<\/span><\/span>\r\ncalories <span class=\"token operator\">=<\/span> <span class=\"token number\">100<\/span>\r\n\r\nprint <span class=\"token string-literal\"><span class=\"token string\">\"\u606d\u559c\u60a8\uff0c\"<\/span><\/span> <span class=\"token operator\">+<\/span> user <span class=\"token operator\">+<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"\uff01\u60a8\u521a\u521a\u5728\u8fd9\u6b21\u953b\u70bc\u4e2d\u71c3\u70e7\u4e86 \"<\/span><\/span> <span class=\"token operator\">+<\/span> calories<mark><span class=\"token punctuation\">.<\/span>to_s<\/mark> <span class=\"token operator\">+<\/span> <span class=\"token string-literal\"><span class=\"token string\">\" \u5361\u8def\u91cc\u3002\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u518d\u6b21\u8fd0\u884c\u7a0b\u5e8f\uff0c\u4f60\u5c06\u4f1a\u5f97\u5230\u9884\u671f\u7684\u8f93\u51fa\u3002<\/p>\n<div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa<\/div>\n<pre class=\"post-pre\"><code>\u606d\u559c\u60a8\uff0cSammy\uff01\u60a8\u521a\u521a\u5728\u8fd9\u6b21\u953b\u70bc\u4e2d\u71c3\u70e7\u4e86 100 \u5361\u8def\u91cc\u3002\r\n<\/code><\/pre>\n<p>\u53e6\u4e00\u4e2a\u9009\u62e9\u662f\u4f7f\u7528Ruby\u7684\u5b57\u7b26\u4e32\u63d2\u503c\u529f\u80fd\uff0c\u5b83\u4f1a\u81ea\u52a8\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u8bf7\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u503c\uff08string interpolation\uff09\u91cd\u5199\u60a8\u7a0b\u5e8f\u7684\u8f93\u51fa\u884c\u3002<\/p>\n<div class=\"code-file-label\">\u5361\u8def\u91cc\u8ba1\u7b97\u5668.rb<\/div>\n<pre class=\"post-pre\"><code>print <span class=\"token string-literal\"><span class=\"token string\">\"\u606d\u559c\uff0c<\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">user<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\">\uff01\u60a8\u5728\u8fd9\u6b21\u953b\u70bc\u4e2d\u71c3\u70e7\u4e86<\/span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{<\/span><span class=\"token content\">calories<\/span><span class=\"token delimiter punctuation\">}<\/span><\/span><span class=\"token string\">\u5361\u8def\u91cc\u3002\"<\/span><\/span>\r\n<\/code><\/pre>\n<p>\u91cd\u65b0\u8fd0\u884c\u7a0b\u5e8f\u3002\u4f60\u4f1a\u6ce8\u610f\u5230\u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u503c\u65b9\u6cd5\u7684\u76f8\u540c\u8f93\u51fa\u3002<\/p>\n<p>Ruby\u5bf9\u8c61\u90fd\u63d0\u4f9b\u4e86\u81ea\u5df1\u7684to_s\u5b9e\u73b0\uff0c\u4f46\u8fd9\u53ef\u80fd\u4e0d\u4e00\u5b9a\u9002\u5408\u8f93\u51fa\u3002\u60a8\u53ef\u80fd\u9700\u8981\u7f16\u5199\u81ea\u5df1\u7684\u4ee3\u7801\u6765\u83b7\u53d6\u6240\u9700\u7684\u8f93\u51fa\uff0c\u6216\u8005\u8c03\u67e5\u5176\u4ed6\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u6570\u636e\u3002<\/p>\n<div class=\"post-conf-note\">\n<p class=\"post-conf-desc\">\u6ce8\u610f<\/p>\n<div>Ruby\u5bf9\u8c61\u8fd8\u63d0\u4f9b\u4e86inspect\u65b9\u6cd5\uff0c\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u975e\u5e38\u6709\u7528\u3002inspect\u65b9\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u548cto_s\u65b9\u6cd5\u7c7b\u4f3c\u3002\u5b83\u901a\u5e38\u8fd4\u56de\u5bf9\u8c61\u53ca\u5176\u6570\u636e\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u5728\u751f\u4ea7\u5e94\u7528\u4e2d\u4e0d\u4f1a\u4f7f\u7528inspect\uff0c\u4f46\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u5728\u67e5\u770b\u53d8\u91cf\u65f6\u4f7f\u7528puts\u548cinspect\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u4f60\u4f1a\u5b66\u4e60\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/div>\n<\/div>\n<h2>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4<\/h2>\n<p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528split\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"one two three\"<\/span><\/span><span class=\"token punctuation\">.<\/span>split\r\n<\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa<\/div>\n<pre class=\"post-pre\"><code>[\"one\", \"two\", \"three\"]<\/code><\/pre>\n<p>\u901a\u8fc7\u5c06\u5b83\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u62c6\u5206\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u8981\u7528\u4f5c\u5206\u9694\u7b26\u7684\u5b57\u7b26\u3002<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3adata_import.rb\u7684\u7a0b\u5e8f\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u7531\u9017\u53f7\u5206\u9694\u7684\u9ca8\u9c7c\u5b57\u7b26\u4e32\u3002\u8be5\u7a0b\u5e8f\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u8fdb\u884c\u6392\u5e8f\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n<pre class=\"post-pre\"><code>data <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"Tiger,Great White,Hammerhead,Whale,Bullhead\"<\/span><\/span>\r\n\r\n<span class=\"token comment\"># \u901a\u8fc7\u9017\u53f7\u5206\u5272\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u7ec4<\/span>\r\nsharks <span class=\"token operator\">=<\/span> data<span class=\"token punctuation\">.<\/span>split<span class=\"token punctuation\">(<\/span><span class=\"token string-literal\"><span class=\"token string\">\",\"<\/span><\/span><span class=\"token punctuation\">)<\/span>\r\n\r\n<span class=\"token comment\"># \u6309\u5b57\u6bcd\u987a\u5e8f\u5bf9\u9ca8\u9c7c\u8fdb\u884c\u6392\u5e8f<\/span>\r\nsharks <span class=\"token operator\">=<\/span> sharks<span class=\"token punctuation\">.<\/span>sort<span class=\"token operator\">!<\/span>\r\n\r\n<span class=\"token comment\"># \u901a\u8fc7\u904d\u5386\u6570\u7ec4\u6253\u5370\u51fa\u9ca8\u9c7c<\/span>\r\nsharks<span class=\"token punctuation\">.<\/span><span class=\"token keyword\">each<\/span><span class=\"token punctuation\">{<\/span><span class=\"token operator\">|<\/span>shark<span class=\"token operator\">|<\/span> puts shark <span class=\"token punctuation\">}<\/span>\r\n<\/code><\/pre>\n<p>\u8fd0\u884c\u7a0b\u5e8f\u3002<\/p>\n<pre class=\"post-pre\"><code>ruby data_import.rb<\/code><\/pre>\n<p>\u8fd9\u662f\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa<\/div>\n<pre class=\"post-pre\"><code>Bullhead\r\nGreat White\r\nHammerhead\r\nTiger\r\nWhale<\/code><\/pre>\n<p>Ruby\u7684\u6570\u7ec4\u662f\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u3002\u8fd9\u5c55\u793a\u4e86\u4f7f\u7528\u6570\u7ec4\u5904\u7406\u6570\u636e\u7684\u4e00\u79cd\u65b9\u5f0f\u3002<\/p>\n<p>\u6700\u540e\uff0c\u4f60\u73b0\u5728\u53ef\u4ee5\u5b66\u4e60\u5982\u4f55\u5728\u5b57\u7b26\u4e32\u548c\u7b26\u53f7\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<h2>\u5728\u5b57\u7b26\u4e32\u548c\u7b26\u53f7\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362<\/h2>\n<p>\u6709\u65f6\u5019\u60a8\u4f1a\u60f3\u8981\u5c06\u4e00\u4e2a\u7b26\u53f7\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u4ee5\u4fbf\u5c55\u793a\u5b83\uff0c\u6709\u65f6\u5019\u60a8\u4e5f\u4f1a\u60f3\u8981\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7b26\u53f7\u4ee5\u4fbf\u5728\u54c8\u5e0c\u8868\u4e2d\u67e5\u627e\u67d0\u4e2a\u4e1c\u897f\u3002<\/p>\n<p>Ruby\u7684to_s\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u5728\u7b26\u53f7\u4e0a\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u5c06\u7b26\u53f7\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"post-pre\"><code><span class=\"token symbol\">:language<\/span><span class=\"token punctuation\">.<\/span>to_s\r\n<\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa<\/div>\n<pre class=\"post-pre\"><code>\"language\"<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u9700\u8981\u663e\u793a\u4e00\u4e2a\u7b26\u53f7\u5e76\u4e14\u60f3\u8981\u6539\u53d8\u5b83\u7684\u5916\u89c2\uff0c\u8fd9\u5c06\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u7a0b\u5e8f\u5c06\u7b26\u53f7:first_name\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6027\u66f4\u9ad8\u7684\u5b57\u7b26\u4e32&#8221;\u540d\u5b57&#8221;\u3002<\/p>\n<pre class=\"post-pre\"><code>string <span class=\"token operator\">=<\/span> <span class=\"token symbol\">:first_name<\/span><span class=\"token punctuation\">.<\/span>to_s\r\n\r\n<span class=\"token comment\"># \u7528\u7a7a\u683c\u66ff\u6362\u4e0b\u5212\u7ebf\u5e76\u9996\u5b57\u6bcd\u5927\u5199<\/span>\r\nstring <span class=\"token operator\">=<\/span> string<span class=\"token punctuation\">.<\/span>gsub<span class=\"token punctuation\">(<\/span><span class=\"token string-literal\"><span class=\"token string\">\"_\"<\/span><\/span><span class=\"token punctuation\">,<\/span><span class=\"token string-literal\"><span class=\"token string\">\" \"<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>capitalize\r\n<\/code><\/pre>\n<p>\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u7b26\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528 to_sym \u65b9\u6cd5\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre class=\"post-pre\"><code><span class=\"token string-literal\"><span class=\"token string\">\"first_name\"<\/span><\/span><span class=\"token punctuation\">.<\/span>to_sym\r\n<\/code><\/pre>\n<div class=\"secondary-code-label\" title=\"\u8f93\u51fa\">\u8f93\u51fa<\/div>\n<pre class=\"post-pre\"><code>:first_name<\/code><\/pre>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>:first_name<\/p>\n<pre class=\"post-pre\"><code><\/code><\/pre>\n<p>\u5c06\u5b57\u7b26\u4e32&#8221;First name&#8221;\u8f6c\u6362\u4e3a\u7b26\u53f7:first_name\uff0c\u9700\u5c06\u6240\u6709\u5b57\u6bcd\u6539\u4e3a\u5c0f\u5199\uff0c\u5e76\u7528\u4e0b\u5212\u7ebf\u66ff\u6362\u7a7a\u683c\u3002<\/p>\n<pre class=\"post-pre\"><code>string <span class=\"token operator\">=<\/span> <span class=\"token string-literal\"><span class=\"token string\">\"First name\"<\/span><\/span>\r\n\r\n<span class=\"token comment\"># \u7528\u4e0b\u5212\u7ebf\u66ff\u6362\u7a7a\u683c\u5e76\u8f6c\u6362\u4e3a\u5c0f\u5199<\/span>\r\nstring <span class=\"token operator\">=<\/span> string<span class=\"token punctuation\">.<\/span>gsub<span class=\"token punctuation\">(<\/span><span class=\"token string-literal\"><span class=\"token string\">\" \"<\/span><\/span><span class=\"token punctuation\">,<\/span><span class=\"token string-literal\"><span class=\"token string\">\"_\"<\/span><\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span>downcase\r\n\r\n<span class=\"token comment\"># \u8f6c\u6362\u4e3a\u7b26\u53f7<\/span>\r\nsymbol <span class=\"token operator\">=<\/span> string<span class=\"token punctuation\">.<\/span>to_sym \r\n<\/code><\/pre>\n<p>\u65e0\u8bba\u662f\u5728\u5c4f\u5e55\u4e0a\u4ee5\u4eba\u7c7b\u53cb\u597d\u7684\u683c\u5f0f\u663e\u793a\u7b26\u53f7\uff0c\u8fd8\u662f\u5728\u4f7f\u7528\u7b26\u53f7\u4f5c\u4e3a\u952e\u7684\u54c8\u5e0c\u4e2d\u901a\u8fc7\u5b57\u7b26\u4e32\u67e5\u627e\u952e\uff0c\u60a8\u90fd\u4f1a\u53d1\u73b0\u6709\u65f6\u9700\u8981\u8fdb\u884c\u8fd9\u4e9b\u8f6c\u6362\u3002<\/p>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u672c\u6559\u7a0b\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u65b9\u6cd5\u5c06\u51e0\u79cd\u91cd\u8981\u7684Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u6210\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u3002\u73b0\u5728\u60a8\u53ef\u4ee5\u628a\u6570\u5b57\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4\uff0c\u5e76\u5728\u7b26\u53f7\u548c\u5b57\u7b26\u4e32\u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<p>\u67e5\u770b\u8fd9\u4e9b\u6559\u7a0b\uff0c\u4ee5\u7ee7\u7eed\u63a2\u7d22Ruby\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<ul class=\"post-ul\">\n<li>\u5982\u4f55\u5728Ruby\u4e2d\u5904\u7406\u5b57\u7b26\u4e32<\/li>\n<li>\u5982\u4f55\u5728Ruby\u4e2d\u5904\u7406\u6570\u7ec4<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00\u5728\u60a8\u521b\u5efa\u7684\u6bcf\u4e2a\u7a0b\u5e8f\u4e2d\uff0c\u5c06\u5305\u542b\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u9700\u8981\u7262\u8bb0\u7684\u662f\uff0c\u901a\u5e38\u60a8\u5c06\u5728\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e2d\u6267\u884c\u64cd\u4f5c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u60a8 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[258,1683,612,184,307],"class_list":["post-585","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ruby","tag-612","tag-184","tag-307"],"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\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecdRuby\u4e2d\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u6570\u7ec4\u7b49\u5e38\u89c1\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9Ruby\u5f00\u53d1\u8005\u638c\u63e1\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u89e3\u51b3\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u95ee\u9898\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\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecdRuby\u4e2d\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u6570\u7ec4\u7b49\u5e38\u89c1\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9Ruby\u5f00\u53d1\u8005\u638c\u63e1\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u89e3\u51b3\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u95ee\u9898\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ruby\u4e2d\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:published_time\" content=\"2023-12-02T16:32:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-02T12:24:00+00:00\" \/>\n<meta name=\"author\" content=\"\u6e05, \u626c\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u6e05, \u626c\" \/>\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%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/\",\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/\",\"name\":\"Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#website\"},\"datePublished\":\"2023-12-02T16:32:00+00:00\",\"dateModified\":\"2025-08-02T12:24:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/cb5556d2501da73d864cac945e8d9461\"},\"description\":\"\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecdRuby\u4e2d\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u6570\u7ec4\u7b49\u5e38\u89c1\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9Ruby\u5f00\u53d1\u8005\u638c\u63e1\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u89e3\u51b3\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u95ee\u9898\u3002\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/#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%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.silicloud.com\/zh\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a\"}]},{\"@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\/cb5556d2501da73d864cac945e8d9461\",\"name\":\"\u6e05, \u626c\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/32a4239de8ff29adace466261d309424a1e5fe9f7e3036bf89fe03f2e3dbe717?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/32a4239de8ff29adace466261d309424a1e5fe9f7e3036bf89fe03f2e3dbe717?s=96&d=mm&r=g\",\"caption\":\"\u6e05, \u626c\"},\"url\":\"https:\/\/www.silicloud.com\/zh\/blog\/author\/qingyang\/\"},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/#local-main-organization-logo\",\"url\":\"\",\"contentUrl\":\"\",\"caption\":\"Blog - Silicon Cloud\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a - Blog - Silicon Cloud","description":"\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecdRuby\u4e2d\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u6570\u7ec4\u7b49\u5e38\u89c1\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9Ruby\u5f00\u53d1\u8005\u638c\u63e1\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u89e3\u51b3\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u95ee\u9898\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\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\/","og_locale":"zh_CN","og_type":"article","og_title":"Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a","og_description":"\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecdRuby\u4e2d\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u6570\u7ec4\u7b49\u5e38\u89c1\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9Ruby\u5f00\u53d1\u8005\u638c\u63e1\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u89e3\u51b3\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u95ee\u9898\u3002","og_url":"https:\/\/www.silicloud.com\/zh\/blog\/\u5982\u4f55\u5728ruby\u4e2d\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\/","og_site_name":"Blog - Silicon Cloud","article_published_time":"2023-12-02T16:32:00+00:00","article_modified_time":"2025-08-02T12:24:00+00:00","author":"\u6e05, \u626c","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u6e05, \u626c","\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%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/","url":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/","name":"Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#website"},"datePublished":"2023-12-02T16:32:00+00:00","dateModified":"2025-08-02T12:24:00+00:00","author":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/cb5556d2501da73d864cac945e8d9461"},"description":"\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecdRuby\u4e2d\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u6570\u7ec4\u7b49\u5e38\u89c1\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u5e2e\u52a9Ruby\u5f00\u53d1\u8005\u638c\u63e1\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u89e3\u51b3\u5b9e\u9645\u5f00\u53d1\u4e2d\u7684\u95ee\u9898\u3002","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/#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%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.silicloud.com\/zh\/blog\/"},{"@type":"ListItem","position":2,"name":"Ruby\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u5b8c\u5168\u6307\u5357\uff1a\u4ece\u5165\u95e8\u5230\u7cbe\u901a"}]},{"@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\/cb5556d2501da73d864cac945e8d9461","name":"\u6e05, \u626c","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/32a4239de8ff29adace466261d309424a1e5fe9f7e3036bf89fe03f2e3dbe717?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/32a4239de8ff29adace466261d309424a1e5fe9f7e3036bf89fe03f2e3dbe717?s=96&d=mm&r=g","caption":"\u6e05, \u626c"},"url":"https:\/\/www.silicloud.com\/zh\/blog\/author\/qingyang\/"},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.silicloud.com\/zh\/blog\/%e5%a6%82%e4%bd%95%e5%9c%a8ruby%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/#local-main-organization-logo","url":"","contentUrl":"","caption":"Blog - Silicon Cloud"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/585","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\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/comments?post=585"}],"version-history":[{"count":4,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/585\/revisions"}],"predecessor-version":[{"id":110310,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/posts\/585\/revisions\/110310"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/media?parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/categories?post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/zh\/blog\/wp-json\/wp\/v2\/tags?post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}