{"id":2675,"date":"2021-10-28T07:50:47","date_gmt":"2021-10-27T22:50:47","guid":{"rendered":"https:\/\/suzutukiblog.com\/?p=2675"},"modified":"2022-01-20T11:17:03","modified_gmt":"2022-01-20T02:17:03","slug":"ruby","status":"publish","type":"post","link":"https:\/\/suzutukiblog.com\/index.php\/2021\/10\/28\/ruby\/","title":{"rendered":"Ruby\u306e\u307e\u3068\u30811( \u6982\u8981\u304b\u3089\u6f14\u7b97\u5b50,\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u307e\u3067)"},"content":{"rendered":"<h3>Ruby\u306e\u7279\u5fb4(\u6982\u8981)<\/h3>\n<p>\u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u578b\u8a00\u8a9e<\/p>\n<p>\u65e5\u672c\u4eba\u306e\u307e\u3064\u3082\u3068\u3086\u304d\u3072\u308d\u3055\u3093\u304c\u4f5c\u3063\u305f\u8a00\u8a9e<\/p>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u8a00\u8a9e<\/p>\n<p>\u69cb\u6587\u306e\u81ea\u7531\u5ea6\u304c\u9ad8\u3044<\/p>\n<p>Ruby on Rails\u3068\u3044\u3046\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u3042\u308b<\/p>\n<pre>irb\u3092\u4f7f\u3046\u3068\u53cc\u65b9\u5411\u7684\u306a\u3084\u308a\u53d6\u308a\u304c\u3067\u304d\u308b\r\n2.5.1 :001 &gt; s = 'hello, World!'\r\n=&gt; \"hello, World!\"\r\n2.5.1 :002 &gt; puts s\r\nhello, World!\r\n=&gt; nil\r\n2.5.1 :003 &gt; n = 10 * 2\r\n=&gt; 20\r\n2.5.1 :004 &gt; puts n\r\n20\r\n=&gt; nil<\/pre>\n<p>pry\u3092\u4f7f\u3063\u3066\u3082\u3067\u304d\u308b<\/p>\n<pre>\u2193\u5909\u6570\u306b\u521d\u671f\u5024\u3092\u5165\u308c\u306a\u3044\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308b\r\n2.5.1 :005 &gt; i\r\nTraceback (most recent call last):\r\n2: from \/home\/ec2-user\/.rvm\/rubies\/ruby-2.5.1\/bin\/irb:11:in `&lt;main&gt;'\r\n1: from (irb):5\r\nNameError (undefined local variable or method `i' for main:Object)<\/pre>\n<p>\u5909\u6570\u306bnil\u3092\u5165\u308c\u308b\u3053\u3068\u3082\u3067\u304d\u308b<\/p>\n<pre>2.5.1 :006 &gt; i = nil\r\n=&gt; nil<\/pre>\n<p>\u4e88\u7d04\u8a9e\u306f\u4f7f\u3048\u306a\u3044\u306e\u3067\u6ce8\u610f\u3000<strong>if elsif<\/strong> \u306a\u3069<\/p>\n<p>\u826f\u304f\u306a\u3044\u5909\u6570\u306e\u4f7f\u3044\u65b9<br \/>\n\u5909\u6570\u306b\u8907\u6570\u5165\u308c\u308b<br \/>\n<!--more--><\/p>\n<pre>x = 'abc'\r\n=&gt; \"abc\"\r\n2.5.1 :002 &gt; x = 123\r\n=&gt; 123\r\n2.5.1 :003 &gt; x = 'def'\r\n=&gt; \"def\"\r\n2.5.1 :004 &gt; x = [1,2,3]\r\n=&gt; [1, 2, 3]\r\n2.5.1 :005 &gt; i, j =1, 2\r\n=&gt; [1, 2]\r\n2.5.1 :006 &gt; i\r\n=&gt; 1\r\n2.5.1 :007 &gt; j\r\n=&gt; 2\r\n2.5.1 :008 &gt; a = b = 10\r\n=&gt; 10\r\n2.5.1 :009 &gt; a\r\n=&gt; 10\r\n2.5.1 :010 &gt; b\r\n=&gt; 10<\/pre>\n<p>\u5b9a\u6570\u306f\u5168\u3066\u5927\u6587\u5b57\u3067\u5909\u5316\u3057\u306a\u3044\u3082\u306e\u3084\u983b\u7e41\u306b\u5909\u308f\u3089\u306a\u3044\u5024\u306a\u3069\u306b\u3059\u308b<br \/>\n\uff08\u624b\u6570\u6599\u7387\u3001\u6d88\u8cbb\u7a0e\u7387\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u4fa1\u683c\u3001\u5186\u5468\u7387\u306a\u3069)<\/p>\n<p>\u5b9a\u6570\u3078\u306e\u4ee3\u5165\u306f\u4e00\u5ea6\u304d\u308a\u3067\u307e\u305f\u4ee3\u5165\u3059\u308b\u3068\u8b66\u544a\u6587\u304c\u8868\u793a\u3055\u308c\u308b\u304c<br \/>\n\u518d\u4ee3\u5165\u3067\u304d\u3066\u3057\u307e\u3046\u306e\u3067\u6ce8\u610f\uff01<\/p>\n<h3><strong>\u30ea\u30c6\u30e9\u30eb<\/strong>\u3068\u306f<\/h3>\n<p>\u6570\u5024\u306e123 \u6587\u5b57\u5217\u306e&#8217;Hello World&#8217;\u306a\u3069<br \/>\nRuby\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4e2d\u306b\u76f4\u63a5\u8a18\u8ff0\u3067\u304d\u308b\u5024\u306e\u3053\u3068\u3092<strong>\u30ea\u30c6\u30e9\u30eb<\/strong>\u3068\u8a00\u3046<\/p>\n<p>\u4ed6\u306b\u3082\u2193\u306a\u3069\u304c\u3042\u308b<br \/>\n\u30fb\u914d\u5217[1,2,3]<\/p>\n<p>\u30fb\u30cf\u30c3\u30b7\u30e5\u3000(\u9023\u60f3\u914d\u5217)\uff5b &#8216;red&#8217; =&gt; &#8216;#ff0000&#8217;}<\/p>\n<h3>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306e\u6982\u8981<\/h3>\n<p><strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/strong>\u3068\u306f<\/p>\n<p>\u30fb\u30c7\u30fc\u30bf\u3068\u51e6\u7406\u306e\u96c6\u307e\u308a\u306e\u3053\u3068<\/p>\n<p><strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411<\/strong>\u3068\u306f<\/p>\n<p>\u30fb\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u540c\u58eb\u304c\u76f8\u4e92\u306b\u95a2\u4fc2\u3057\u3042\u3046\u3053\u3068\u3067<br \/>\n\u30b7\u30b9\u30c6\u30e0\u306e\u632f\u308b\u821e\u3044\u3092\u6349\u3048\u308b\u8003\u3048\u65b9<\/p>\n<p><strong>\u30af\u30e9\u30b9<\/strong>\u3068\u306f<\/p>\n<p><strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8<\/strong>\u306e\u8a2d\u8a08\u56f3<\/p>\n<p><strong>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9<\/strong>\u3068\u306f<\/p>\n<p>\u30fb<strong>\u30af\u30e9\u30b9<\/strong>\uff08\u8a2d\u8a08\u56f3\uff09\u304b\u3089\u4f5c\u6210\u3057\u305f\u3001\u5b9f\u614b\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u2252\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9<\/p>\n<p><strong>\u30e1\u30bd\u30c3\u30c9<\/strong>\u3068\u306f<\/p>\n<p>\u30fb\u30af\u30e9\u30b9\u306e\u4e2d\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3082\u306e\u3067\u3001<\/p>\n<p>\u8907\u6570\u306e\u51e6\u7406\u3092\u4e00\u3064\u306b\u307e\u3068\u3081\u3066\u3001\u6271\u3044\u3084\u3059\u304f\u3057\u305f\u3082\u306e\u3002<br \/>\n\u30fb\u30af\u30e9\u30b9\u306b\u6240\u5c5e\u3059\u308b\u95a2\u6570\u306e\u3088\u3046\u306a\u3082\u306e<\/p>\n<h3><strong>\u52d5\u7684\u578b\u4ed8\u3051\u3068\u306f\uff1f<\/strong><\/h3>\n<p>\u5909\u6570\u306b\u5024\u3092\u4ee3\u5165\u3059\u308b\u6642\u306b\u3001\u52d5\u7684\u578b\u4ed8\u3051\u304c\u3055\u308c\u308b<br \/>\n\u30fb\u5909\u6570\u306e\u578b\u6307\u5b9a\u3092\u5909\u6570\u306e\u5ba3\u8a00\u6642\u306b\u884c\u308f\u306a\u304f\u3066\u3044\u3044\u3068\u3044\u3046\u3053\u3068<\/p>\n<p>OK: i \uff1d1<br \/>\nNG: int i = 0<\/p>\n<p>\u5c0f\u6570\u70b9\u304c\u3064\u3044\u3066\u306a\u3044\u3068\u2193\u6b63\u3057\u304f\u8a08\u7b97\u3055\u308c\u306a\u3044\u306e\u3067\u6ce8\u610f<\/p>\n<pre>2.5.1 :012 &gt; 3 \/ 2\r\n=&gt; 1\r\n2.5.1 :013 &gt; 3.0 \/ 2\r\n=&gt; 1.5\r\n2.5.1 :015 &gt; 3 \/ 2.0\r\n=&gt; 1.5\r\n2.5.1 :016 &gt; 3.0 \/ 2.0\r\n=&gt; 1.5\r\n10 ** 3\r\n=&gt; 1000<\/pre>\n<pre>f\u306ffloat\u3067\u5c0f\u6570\u70b9\u3092\u6271\u3046\u6642\u306b\u4f7f\u3046\u2193\r\nx = 3\r\n=&gt; 3\r\n2.5.1 :020 &gt; x.to_f\r\n=&gt; 3.0\r\n2.5.1 :021 &gt; x.to_f \/ 2\r\n=&gt; 1.5\r\n2.5.1 :022 &gt; x \/ 2\r\n=&gt; 1\r\nx \/ y.to_f\r\n=&gt; 1.5<\/pre>\n<p>class\u3092\u3064\u3051\u308b\u3053\u3068\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u306a\u3093\u306e\u30af\u30e9\u30b9\u304b\u308f\u304b\u308b<\/p>\n<pre>1.class\r\n=&gt; Integer\r\n2.5.1 :027 &gt; 1.1.class\r\n=&gt; Float\r\n\u4f7f\u3048\u308b\u6a5f\u80fd\u304c\u308f\u304b\u308b\r\n1.methods\r\n=&gt; [:-@, :**, :&lt;=&gt;, :upto, :&lt;&lt;, :&lt;=, :&gt;=, :==, :chr, :===, :&gt;&gt;, :[], :%, :&amp;, :inspect, :+, :ord, :-, :\/, :*, :size, :succ, :&lt;, :&gt;, :to_int, :coerce, :divmod, :to_s, :to_i, :fdiv, :modulo, :remainder, :abs, :magnitude, :integer?, :numerator, :denominator, :to_r, :floor, :ceil, :round, :truncate, :gcd, :to_f, :^, :odd?, :even?, :allbits?, :anybits?, :nobits?, :downto, :times, :pred, :pow, :bit_length, :digits, :rationalize, :lcm, :gcdlcm, :next, :div, :|, :~, :+@, :eql?, :singleton_method_added, :i, :real?, :zero?, :nonzero?, :finite?, :infinite?, :step, :positive?, :negative?, :arg, :rectangular, :rect, :real, :imag, :abs2, :imaginary, :angle, :phase, :conjugate, :to_c, :polar, :conj, :clone, :dup, :quo, :between?, :clamp, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :instance_variable_get, :public_methods, :instance_variables, :method, :public_method, :singleton_method, :define_singleton_method, :public_send, :extend, :to_enum, :enum_for, :pp, :=~, :!~, :respond_to?, :freeze, :object_id, :send, :display, :nil?, :hash, :class, :singleton_class, :itself, :yield_self, :taint, :untaint, :tainted?, :untrusted?, :untrust, :frozen?, :trust, :singleton_methods, :methods, :private_methods, :protected_methods, :!, :equal?, :instance_eval, :instance_exec, :!=, :__id__, :__send__]\r\n\r\n<\/pre>\n<h3>&#8216;(\u30b7\u30f3\u30b0\u30eb\u30af\u30a9\u30fc\u30c8)\u3068&#8221;(\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c8)\u306e\u9055\u3044<\/h3>\n<pre>\u6539\u884c\u3055\u308c\u308b\u2193\r\nputs \"ruby\\nProgramming\"\r\nruby\r\nProgramming\r\n=&gt; nil\r\n2.5.1 :031 &gt; puts 'ruby\\nProgramming'\r\nruby\\nProgramming\r\n\u2191\u3055\u308c\u306a\u3044<\/pre>\n<pre>puts \"My name is #{first_name} #{last_name}.\"\r\nMy name is suzutuki aki.\r\n=&gt; nil\r\n2.5.1 :036 &gt; puts 'My name is #{first_name} #{last_name}.'\r\nMy name is #{first_name} #{last_name}.\r\n=&gt; nil<\/pre>\n<h3>\u3064\u307e\u308a\u3069\u3046\u3044\u3046\u3053\u3068\uff1f<\/h3>\n<p>\u7279\u6b8a\u6587\u5b57\u3084\u5f0f\u5c55\u958b\u3059\u308b\u3068\u304d\u306f&#8221;&#8221;<br \/>\n\u305d\u308c\u4ee5\u5916\u306f&#8221;\u3092\u4f7f\u3046\uff01<\/p>\n<p>.upcase\u306f\u5168\u3066\u5927\u6587\u5b57\u3067\u51fa\u529b\u3055\u308c\u308b<\/p>\n<pre>'Hello, World!'\r\n=&gt; \"Hello, World!\"\r\n2.5.1 :042 &gt; puts s.upcase\r\nHELLO, WORLD!\r\n=&gt; nil\r\n\u7834\u58ca\u7684\u30e1\u30bd\u30c3\u30c9\r\ns.upcase!\r\n=&gt; \"HELLO, WORLD!\"\r\n2.5.1 :045 &gt; s\r\n\u5909\u6570s\u304c\u3059\u3079\u3066\u5927\u6587\u5b57\u306b\u5909\u308f\u3063\u3066\u3057\u307e\u3063\u305f!\r\nslice\r\n=&gt; \"HELLO, WORLD!\"\r\n'Hello, World!'.slice(0, 3)\r\n=&gt;\"HEL\"\r\n'Hello, World!'.slice(3, 2)\r\n=&gt;\"lo\"<\/pre>\n<p>.downcase(\u5168\u3066\u5c0f\u6587\u5b57\u306b\u3059\u308b)<\/p>\n<p>.size(\u6587\u5b57\u5217\u306e\u9577\u3055\u304c\u51fa\u529b\u3055\u308c\u308b)\u306a\u3069\u3082\u3042\u308b<\/p>\n<pre>\u7a7a\u767d\u6587\u5b57 \u2193\u3053\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u304c\u898b\u306b\u304f\u3044\u306e\u3067NG\r\n\r\n10   +  2    - 1<\/pre>\n<pre>\u6bd4\u8f03\u6f14\u7b97\u5b50\u3000\u6f14\u7b97\u5b50\u306f\u8a18\u53f7\u306e\u3053\u3068\r\n\r\n1 &lt; 2\r\n=&gt; true\r\n2.5.1 :049 &gt; 1 &lt;= 2\r\n=&gt; true\r\n2.5.1 :050 &gt; 1 &gt; 2\r\n=&gt; false\r\n2.5.1 :051 &gt; 1 == 1\r\n=&gt; true\r\n2.5.1 :052 &gt; 1 != 2\r\n=&gt; true<\/pre>\n<pre>\u6f14\u7b97\u5b50\u306e\u512a\u5148\u9806\u4f4d\u3000\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9\u3088\u308a\r\n\r\n\u9ad8\u3044 ::\r\n[]\r\n+(\u5358\u9805) ! ~\r\n**\r\n-(\u5358\u9805)\r\n* \/ %\r\n+ -\r\n&lt;&lt; &gt;&gt;\r\n&amp;\r\n| ^\r\n&gt; &gt;= &lt; &lt;=\r\n&lt;=&gt; == === != =~ !~\r\n&amp;&amp;\r\n||\r\n.. ...\r\n?:(\u6761\u4ef6\u6f14\u7b97\u5b50)\r\n=(+=, -= ... )\r\nnot\r\n\u4f4e\u3044 and or<\/pre>\n<p>\u6570\u5024\u3068\u6587\u5b57\u5217\u306f\u6697\u9ed9\u7684\u306b\u5909\u63db\u3055\u308c\u306a\u3044<\/p>\n<pre>1 + '1'\r\nTraceback (most recent call last):\r\n2: from (irb#1):53\r\n1: from (irb#1):53:in `+'\r\nTypeError (String can't be coerced into Integer)<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u3059\u308c\u3070\u8a08\u7b97\u3067\u304d\u308b<\/p>\n<pre>2.5.1 :054 &gt; 1 + '1'.to_i\r\n=&gt; 2\r\n1 + '1.1'.to_f\r\n=&gt; 2.1\r\n'Number is' + number.to_s\r\n=&gt; \"Number is1\"<\/pre>\n<p>\u30a4\u30f3\u30af\u30ea\u30e1\u30f3\u30c8\u3068\u30c7\u30af\u30ea\u30e1\u30f3\u30c8 += -=<br \/>\nRuby\u3084JavaScript\u306a\u3069\u3067\u3042\u308b\u3000++ &#8212; \u306f\u306a\u3044\u3088<\/p>\n<pre>n = 1\r\n=&gt; 1\r\nn = n + 1\r\n=&gt; 2\r\nn += 1\r\n=&gt; 3\r\nn = n-1\r\n=&gt; 2\r\nn -= 1\r\n=&gt; 1<\/pre>\n<p><a href=\"https:\/\/suzutukiblog.com\/index.php\/2021\/04\/12\/ruby-2\/\">\uff12\u306b\u7d9a\u304f<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ruby\u306e\u7279\u5fb4(\u6982\u8981) \u30a4\u30f3\u30bf\u30d7\u30ea\u30bf\u578b\u8a00\u8a9e \u65e5\u672c\u4eba\u306e\u307e\u3064\u3082\u3068\u3086\u304d\u3072\u308d\u3055\u3093\u304c\u4f5c\u3063\u305f\u8a00\u8a9e \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u8a00\u8a9e \u69cb\u6587\u306e\u81ea\u7531\u5ea6\u304c\u9ad8\u3044 Ruby on Rails\u3068\u3044\u3046\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u3042\u308b irb\u3092\u4f7f\u3046\u3068\u53cc\u65b9\u5411\u7684\u306a\u3084\u308a\u53d6\u308a\u304c\u3067\u304d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27,31],"tags":[],"class_list":["post-2675","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-note","category-ruby"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=2675"}],"version-history":[{"count":8,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2675\/revisions"}],"predecessor-version":[{"id":4477,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2675\/revisions\/4477"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/media\/2684"}],"wp:attachment":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=2675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=2675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=2675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}