{"id":157,"date":"2017-09-27T16:24:02","date_gmt":"2017-09-27T16:24:02","guid":{"rendered":"http:\/\/td_uid_77_59cbd0a2acc25"},"modified":"2022-03-19T23:54:08","modified_gmt":"2022-03-19T16:54:08","slug":"huong-doi-tuong-trong-ruby-phan-1","status":"publish","type":"post","link":"https:\/\/thietkewebdalat.net\/blog\/huong-doi-tuong-trong-ruby-phan-1\/","title":{"rendered":"H\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng trong Ruby &#8211; Ph\u1ea7n 1"},"content":{"rendered":"<p>Xin ch\u00e0o m\u1ecdi ng\u01b0\u1eddi<\/p>\n<p>H\u00f4m nay m\u00ecnh s\u1ebd gi\u1edbi thi\u1ec7u cho c\u00e1c b\u1ea1n v\u1ec1 h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng trong ruby, v\u1ea5n \u0111\u1ec1 n\u00e0y c\u00f3 th\u1ec3 r\u1ea5t nhi\u1ec1u ng\u01b0\u1eddi g\u1eb7p ph\u1ea3i trong qu\u00e1 tr\u00ecnh xin vi\u1ec7c, hi v\u1ecdng sau ph\u1ea7n n\u00e0y s\u1ebd gi\u00fap c\u00e1c b\u1ea1n m\u1ed9t ph\u1ea7n n\u00e0o:<\/p>\n<h2>Classes Define Objects (\u0110\u1ecbnh ngh\u0129a l\u1edbp \u0111\u1ed1i t\u01b0\u1ee3ng)<\/h2>\n<p>Ruby \u0111\u1ecbnh ngh\u0129a c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 h\u00e0nh vi c\u1ee7a c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a n\u00f3 trong c\u00e1c l\u1edbp. B\u1ea1n c\u00f3 th\u1ec3 coi c\u00e1c l\u1edbp nh\u01b0 nh\u1eefng ph\u00e1c th\u1ea3o c\u01a1 b\u1ea3n v\u1ec1 nh\u1eefng g\u00ec m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng n\u00ean \u0111\u01b0\u1ee3c t\u1ea1o ra v\u00e0 nh\u1eefng g\u00ec n\u00f3 c\u00f3 th\u1ec3 l\u00e0m \u0111\u01b0\u1ee3c. \u0110\u1ec3 \u0111\u1ecbnh ngh\u0129a m\u1ed9t l\u1edbp, ch\u00fang ta s\u1eed d\u1ee5ng c\u00fa ph\u00e1p t\u01b0\u01a1ng t\u1ef1 nh\u01b0 \u0111\u1ecbnh ngh\u0129a m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c. V\u00ed d\u1ee5 :<\/p>\n<pre>class RubyOpp\r\nend\r\nbienluutru = RubyOpp.new\r\n<\/pre>\n<h2>Modules<\/h2>\n<p>M\u00f4-\u0111un l\u00e0 m\u1ed9t c\u00e1ch kh\u00e1c \u0111\u1ec3 th\u1ec3 hi\u1ec7n \u0111\u01b0\u1ee3c t\u00ednh \u0111a h\u00ecnh trong Ruby. M\u1ed9t m\u00f4-\u0111un l\u00e0 m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c h\u00e0nh vi c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u0111\u01b0\u1ee3c trong c\u00e1c l\u1edbp kh\u00e1c th\u00f4ng qua c\u00e1c mixin. M\u1ed9t m\u00f4-\u0111un \u0111\u01b0\u1ee3c &#8220;tr\u1ed9n l\u1eabn&#8221; v\u00e0o m\u1ed9t l\u1edbp b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng include. V\u00ed d\u1ee5:<\/p>\n<pre>module Speak\r\n  def speak(sound)\r\n    puts sound\r\n  end\r\nend\r\n\r\nclass GoodDog\r\n  include Speak\r\nend\r\n\r\nclass HumanBeing\r\n  include Speak\r\nend\r\n\r\nsparky = GoodDog.new\r\nsparky.speak(\"Arf!\")        # =&gt; Arf!\r\nbob = HumanBeing.new\r\nbob.speak(\"Hello!\")         # =&gt; Hello!\r\n<\/pre>\n<p>\u0110\u00e2y l\u00e0 2 ph\u1ea7n \u0111\u1ea7u ti\u00ean c\u1ee7a b\u00e0i n\u00e0y,n\u1ebfu c\u00e1c b\u1ea1n c\u00f3 th\u1eafc m\u1eafc g\u00ec xin vui l\u00f2ng \u0111\u1ec3 l\u1ea1i b\u00ecnh lu\u1eadn \u1edf ph\u00eda d\u01b0\u1edbi.Trong b\u00e0i n\u00e0y ch\u00fang ta c\u00f3 s\u1eed d\u1ee5ng include, v\u00ec v\u1eady b\u00e0i sau t\u00f4i s\u1ebd gi\u1ea3i th\u00edch v\u1ec1 include,prepend,extend \u0111\u1ec3 l\u1ee1 nh\u01b0 c\u00e1c b\u1ea1n c\u00f3 g\u1eb7p c\u00f3 th\u1ec3 x\u1eed l\u00fd. C\u1ea3m \u01a1n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xin ch\u00e0o m\u1ecdi ng\u01b0\u1eddi H\u00f4m nay m\u00ecnh s\u1ebd gi\u1edbi thi\u1ec7u cho c\u00e1c b\u1ea1n v\u1ec1 h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng trong ruby, v\u1ea5n \u0111\u1ec1 n\u00e0y c\u00f3 th\u1ec3 r\u1ea5t nhi\u1ec1u ng\u01b0\u1eddi g\u1eb7p ph\u1ea3i trong qu\u00e1 tr\u00ecnh xin vi\u1ec7c, hi v\u1ecdng sau ph\u1ea7n n\u00e0y s\u1ebd gi\u00fap c\u00e1c b\u1ea1n m\u1ed9t ph\u1ea7n n\u00e0o: Classes Define Objects (\u0110\u1ecbnh ngh\u0129a l\u1edbp \u0111\u1ed1i t\u01b0\u1ee3ng) Ruby [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":369,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"amp_status":"","footnotes":""},"categories":[18],"tags":[],"class_list":{"0":"post-157","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-html-5"},"_links":{"self":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":2,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/posts\/157\/revisions\/370"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media\/369"}],"wp:attachment":[{"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thietkewebdalat.net\/blog\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}