<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[W-Learning Note - Medium]]></title>
        <description><![CDATA[Learning Coding - Medium]]></description>
        <link>https://medium.com/w-learning-note?source=rss----b57d554777d2---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>W-Learning Note - Medium</title>
            <link>https://medium.com/w-learning-note?source=rss----b57d554777d2---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 10:23:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/w-learning-note" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[Ruby]Blocks, Procs and Lambdas]]></title>
            <link>https://medium.com/w-learning-note/ruby-blocks-procs-and-lambdas-42c714623528?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/42c714623528</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[closure]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Sat, 18 Jan 2020 06:28:03 GMT</pubDate>
            <atom:updated>2020-12-21T17:08:59.208Z</atom:updated>
            <content:encoded><![CDATA[<p>比較Ruby的Block, Proc, Lambda</p><h4>Block</h4><p>Block主要有兩種寫法 do...end 或是 {...} ，在 | 中的num是傳入block的變數，而根據不同的需求，也可以傳入多個變數</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Fblock%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Fblock%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/06988cb4ec3bea55146c9681c525955a/href">https://medium.com/media/06988cb4ec3bea55146c9681c525955a/href</a></iframe><p>在上面的範例中，block其實是 each 這個method，將每個array中的item傳入block中，並將控制權轉交給block，block本身不是物件，是不能單獨存在的，只能作為 Ruby 指令或呼叫方法時的引數。</p><p>一般要怎麼執行Block呢？可以使用 yield ，下面的範例，當 my_name 這個method在執行到 yield(name) 這一行時，會暫時將控制權轉給block，並將name這個變數傳入block中。</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Fblockyield%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Fblockyield%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f3c945523bbbce1498323661b8bea7b/href">https://medium.com/media/3f3c945523bbbce1498323661b8bea7b/href</a></iframe><h4>Proc</h4><p>最簡單理解Proc的方式，就是想成你把block存成一個變數，也就是將它物件化，讓code可以重複使用，如果要創建一個Proc，就用 Proc.new ，並且給它一組block就可以了。</p><p>要執行的時候， 只要執行這個物件上的 call ，同時也可以帶入參數。</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Fproc%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Fproc%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b87257a19d45613cfd05bcd46fadb5ef/href">https://medium.com/media/b87257a19d45613cfd05bcd46fadb5ef/href</a></iframe><h4>Lamda</h4><p>Lamda和Proc很相似，只有兩點不同</p><ul><li>當傳入的參數，數量和定義的不同時，Proc不會回報error，Lamda會。</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Flamda1%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Flamda1%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c2e0d62eb8f097be5f9e0be788d57a1/href">https://medium.com/media/4c2e0d62eb8f097be5f9e0be788d57a1/href</a></iframe><ul><li>處理 return 時，當你在一個function裡，呼叫一個內含return的proc，因為是將控制權轉交給proc中的block，所以在執行到return時，function會立即return。而如果是lamda的話，則會回到function本身。</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Flamda2%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Flamda2%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7a929ddc9324711541aa2beac46edd6/href">https://medium.com/media/b7a929ddc9324711541aa2beac46edd6/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fbutton.like.co%2Fin%2Fembed%2Fwendyhj%2Fbutton&amp;display_name=LikeCoin&amp;url=https%3A%2F%2Fbutton.like.co%2Fwendyhj&amp;image=https%3A%2F%2Fstatic.like.co%2Flikecoin_de-portrait.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=like" width="485" height="212" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd30f89b074f3d1562e687e26ba280b8/href">https://medium.com/media/cd30f89b074f3d1562e687e26ba280b8/href</a></iframe><p>參考資料</p><ul><li><a href="https://railsbook.tw/chapters/07-ruby-basic-3.html">方法與程式碼區塊（block） | 高見龍</a></li><li><a href="https://blog.appsignal.com/2018/09/04/ruby-magic-closures-in-ruby-blocks-procs-and-lambdas.html">Closures in Ruby: Blocks, Procs and Lambdas | AppSignal Blog</a></li><li><a href="https://scotch.io/tutorials/understanding-ruby-closures">Understanding Ruby Closures</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42c714623528" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/ruby-blocks-procs-and-lambdas-42c714623528">[Ruby]Blocks, Procs and Lambdas</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Ruby] 實體方法Instance Method與類別方法Class Method]]></title>
            <link>https://medium.com/w-learning-note/ruby-%E5%AF%A6%E9%AB%94%E6%96%B9%E6%B3%95instance-method%E8%88%87%E9%A1%9E%E5%88%A5%E6%96%B9%E6%B3%95class-method-fc69450b4789?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/fc69450b4789</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[instance-method]]></category>
            <category><![CDATA[class-method]]></category>
            <category><![CDATA[中文]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Sat, 14 Sep 2019 15:39:58 GMT</pubDate>
            <atom:updated>2020-12-21T17:13:41.897Z</atom:updated>
            <content:encoded><![CDATA[<p>顧名思義，class method就是class呼叫的，instance method就是instance呼叫的(廢話)，直接先來看個例子:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Fclassinstancemethod%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Fclassinstancemethod%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f316174c8405fd6946d6ce3291ea6daf/href">https://medium.com/media/f316174c8405fd6946d6ce3291ea6daf/href</a></iframe><p>hello 是class method，所以可以用Welcome class呼叫，而 hello_2 是instance method，用Welcome class呼叫會跳出undefined method的error，而使用Welcome.new這個instance呼叫這兩個method，則情況相反。</p><h4>Class Methods</h4><p>有幾種方法可以定義class method</p><pre># 1<br>class Foo<br>  def self.bar<br>    puts &#39;This is class method&#39;<br>  end<br>end</pre><pre># 2<br>class Foo<br>  class &lt;&lt; self<br>    def bar<br>      puts &#39;This is class method&#39;<br>    end<br>  end<br>end</pre><pre># 3<br>class Foo; end<br>def Foo.bar<br>  puts &#39;This is class method&#39;<br>end</pre><p>這三種用 Foo.bar 呼叫的結果都一樣，前兩種是較常見的定義方式，第一種能一眼看出是class method，第二種在class &lt;&lt; self 這個block中，可以定義多個class method，但相較之下也比較難一眼看出是class method還是instance method，而最後一種方式則比較少見。</p><h4>Instance Methods</h4><pre># 1<br>class Foo<br>  def bar<br>    puts &#39;This is instance method&#39;<br>  end<br>end</pre><pre>Foo.new.bar</pre><pre># 2<br>class Foo<br>  attr_accessor :bar<br>end<br><br>foo = Foo.new<br>foo.bar = &#39;This is instance method&#39;<br>puts foo.bar</pre><pre># 3<br>class Foo; end<br><br>foo = Foo.new<br>def foo.bar<br>  puts &#39;This is instance method&#39;<br>end<br><br>Foo.new.bar </pre><p>這三種方式一樣也能得到同樣的output，重點在於instance method需要用instance呼叫，所以需要先創造一個新的instance(Foo.new)</p><h4>使用時機</h4><p>當方法不會和特定的instance有關時，就可以使用class method，例如 ActiveRecord::Base的find 就是一個例子: User.find(id) 。</p><p>那麼，當方法需要和某個特定的instance綁在一起時，就需要用instance method，例如destroy : jack=User.find(1); jack.destroy</p><p>以上是很簡單的對class method和instance method做比較，網路上有很多大神都有寫過這樣相關的文章，也可以看出這是Ruby新手常常會搞混的地方啊😭</p><p>參考:</p><ul><li><a href="https://motion-express.com/blog/20141208-class-method-and-instance-method">Ruby當中的class method和instance method差在哪？</a></li><li><a href="http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/">Class and Instance Methods in Ruby</a></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fbutton.like.co%2Fin%2Fembed%2Fwendyhj%2Fbutton&amp;display_name=LikeCoin&amp;url=https%3A%2F%2Fbutton.like.co%2Fwendyhj&amp;image=https%3A%2F%2Fstatic.like.co%2Flikecoin_de-portrait.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=like" width="485" height="212" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd30f89b074f3d1562e687e26ba280b8/href">https://medium.com/media/cd30f89b074f3d1562e687e26ba280b8/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc69450b4789" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/ruby-%E5%AF%A6%E9%AB%94%E6%96%B9%E6%B3%95instance-method%E8%88%87%E9%A1%9E%E5%88%A5%E6%96%B9%E6%B3%95class-method-fc69450b4789">[Ruby] 實體方法Instance Method與類別方法Class Method</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[Ruby] 變數(Variable)與常數(Constant)]]></title>
            <link>https://medium.com/w-learning-note/ruby-%E8%AE%8A%E6%95%B8-variable-%E8%88%87%E5%B8%B8%E6%95%B8-constant-8decdf75d151?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/8decdf75d151</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[variables]]></category>
            <category><![CDATA[中文]]></category>
            <category><![CDATA[constant]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Wed, 21 Aug 2019 13:58:22 GMT</pubDate>
            <atom:updated>2019-08-21T13:57:32.363Z</atom:updated>
            <content:encoded><![CDATA[<h3>變數</h3><h4>區域變數Local Variable</h4><ul><li>以小寫字母或 _開頭</li><li>scope: class , module , def , do...end , {} 中</li><li>沒有預設值</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Flocalvariable%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Flocalvariable%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec607a7dbd9e553646655facfb77a4cb/href">https://medium.com/media/ec607a7dbd9e553646655facfb77a4cb/href</a></iframe><h4>實例變數Instance Variable</h4><ul><li>以 @ 開頭</li><li>scope: 在相同的object中，所有methods都可以使用。</li><li>預設值為 nil</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Finstancevariable%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Finstancevariable%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fa2a6abc28f914960497aa70b59a1a7/href">https://medium.com/media/5fa2a6abc28f914960497aa70b59a1a7/href</a></iframe><h4>類別變數Class Variable</h4><ul><li>以 @@ 開頭</li><li>scope: class和他的subclass, instance</li><li>沒有預設值</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Fclassvariable%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Fclassvariable%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/25ef7ce4a51f62b0d5c12dd4bf76988d/href">https://medium.com/media/25ef7ce4a51f62b0d5c12dd4bf76988d/href</a></iframe><h4>全域變數Global Variable</h4><ul><li>以 $ 開頭</li><li>scope: 可在任何地方使用</li><li>預設值為nil</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Fglobalvariable%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Fglobalvariable%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/35578ea0564192a0f1b82a9a49b3b869/href">https://medium.com/media/35578ea0564192a0f1b82a9a49b3b869/href</a></iframe><h4>虛擬變數<strong>Pseudo Variable</strong></h4><p>Ruby有一種稱為虛擬變數，是Ruby自定義的，看起來像local variable，但實際上你不能assign任何值給它，像是 self , nil , true , false 都是。</p><h3>常數Constant</h3><ul><li>以大寫字母(A-Z)開頭</li><li>scope: 在class或module中定義的常數，只能在該class或module中使用，在外面定義的則是global的。</li><li>沒有initialize的常數會產生error</li><li>所以的class和module都必須是常數</li><li>Ruby的常數是可以修改的，只是會跳出警告</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Fconstant%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Fconstant%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/607e994a587ab39666595fb989e9342d/href">https://medium.com/media/607e994a587ab39666595fb989e9342d/href</a></iframe><p>參考：</p><ul><li><a href="https://www.w3resource.com/ruby/ruby-variables-constants.php">Ruby Variables, Constants - w3resource</a></li><li><a href="https://www.tutorialspoint.com/ruby/ruby_variables.htm">Ruby - Variables, Constants and Literals</a></li><li><a href="https://pjchender.github.io/2017/09/26/ruby-%E8%AE%8A%E6%95%B8%EF%BC%88variable%EF%BC%89%E8%88%87%E5%B8%B8%E6%95%B8%EF%BC%88constant%EF%BC%89/">[Ruby] 變數（variable）與常數（Constant）</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8decdf75d151" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/ruby-%E8%AE%8A%E6%95%B8-variable-%E8%88%87%E5%B8%B8%E6%95%B8-constant-8decdf75d151">[Ruby] 變數(Variable)與常數(Constant)</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[物件導向中的建構式 (Constructor)]]></title>
            <link>https://medium.com/w-learning-note/%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91%E4%B8%AD%E7%9A%84%E5%BB%BA%E6%A7%8B%E5%BC%8F-constructor-c6d682178dfd?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/c6d682178dfd</guid>
            <category><![CDATA[中文]]></category>
            <category><![CDATA[constructor]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Sat, 03 Aug 2019 10:30:49 GMT</pubDate>
            <atom:updated>2019-08-03T10:23:13.635Z</atom:updated>
            <content:encoded><![CDATA[<p>Constructor 又被稱為建構子、建構式，是一個Class在建立Object時會被呼叫的方法。他能初始化一個新建的Object，並且時常會接收參數並設定初始值(Parameterized constructors)。</p><p>那麼，在Ruby中要怎麼寫建構式呢?</p><pre>class ClassName<br>  def initialize(parameters)<br>    <br>  end<br>end</pre><p>當用new method去建立object時，會在內部呼叫initialize，所有傳給new的 argument會自動傳入initialize ，所有初始值可以用在其他的method中。</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2Fconstructor%3Flite%3Dtrue&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2Fconstructor%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/978eaa2676ee0a38f24928d96615063f/href">https://medium.com/media/978eaa2676ee0a38f24928d96615063f/href</a></iframe><p>參考:</p><p><a href="https://zh.wikipedia.org/wiki/%E6%9E%84%E9%80%A0%E5%99%A8#%E5%8F%82%E6%95%B0%E5%8C%96%E6%9E%84%E9%80%A0%E5%99%A8">Wikipedia</a></p><p><a href="https://www.geeksforgeeks.org/ruby-constructors/">GeeksforGeeks</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6d682178dfd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91%E4%B8%AD%E7%9A%84%E5%BB%BA%E6%A7%8B%E5%BC%8F-constructor-c6d682178dfd">物件導向中的建構式 (Constructor)</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[什麼是物件導向中的封裝、繼承和多型特性？]]></title>
            <link>https://medium.com/w-learning-note/%E4%BB%80%E9%BA%BC%E6%98%AF%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91%E4%B8%AD%E7%9A%84%E5%B0%81%E8%A3%9D-%E7%B9%BC%E6%89%BF%E5%92%8C%E5%A4%9A%E5%9E%8B%E7%89%B9%E6%80%A7-c15d4e8c567a?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/c15d4e8c567a</guid>
            <category><![CDATA[中文]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Sat, 03 Aug 2019 06:48:57 GMT</pubDate>
            <atom:updated>2020-12-21T17:12:35.724Z</atom:updated>
            <content:encoded><![CDATA[<ul><li><strong>封裝 (Encapsulation) :</strong></li></ul><p>隱藏某一方法的具體執行步驟，把過程和資料包起來，對資料的操作只能通過<strong>已定義的介面</strong>(Interface)，封裝的目的，是讓其他人可以使用物件，但不需要知道物件的內部邏輯。</p><p>方法在Ruby中預設是public，而內部需要隱藏的方法，則使用private隱藏起來，這表示你只能使用object中的public method來呼叫他，換句話說，你不能用外部的receiver來呼叫( object.method, object is the receiver of the method)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*6G25tGA4xlKX9N0G.png" /><figcaption><strong>encapsulation</strong></figcaption></figure><ul><li><strong>繼承 (Inheritance):</strong></li></ul><p>讓一個類別(子類別subclass)繼承另一個類別(父類別superclass)的方法和屬性，透過繼承可以達到DRY的目的，也可以適當的切割類別，又不破壞原先基底類別設計。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*i9erK5L0mjesQtA0.png" /><figcaption><strong>inheritance</strong></figcaption></figure><p><em>Note: super 可以去呼叫 superclass 裡面的同名方法</em></p><ul><li><strong>多型 (Polymorphism):</strong></li></ul><p>不同型態的物件，定義相同的操作介面，由於被呼叫者 (Callee) 有著相同的介面，呼叫者並不用指定特別型別，只需針對介面進行操作。</p><p>以下面例子，每個物件都有description方法，但裡面的內容不同，我們可以用同個方法讓性別不同的人都可以呼叫description作自我介紹。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*dautoUGl4ZN_KheV.png" /><figcaption><strong>polymorphism</strong></figcaption></figure><p>參考：</p><p><a href="http://mropengate.blogspot.com/2015/11/software-engineering-oop.html">物件導向設計：從需求轉程式 Object Oriented Design (OOD) : from Requirements to Code</a></p><p><a href="https://www.codecademy.com/learn/learn-ruby">Codecademy: Learn Ruby</a></p><p><a href="http://stan-blog.logdown.com/posts/471289-what-is-the-inheritance">封裝、繼承、多型</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fbutton.like.co%2Fin%2Fembed%2Fwendyhj%2Fbutton&amp;display_name=LikeCoin&amp;url=https%3A%2F%2Fbutton.like.co%2Fwendyhj&amp;image=https%3A%2F%2Fstatic.like.co%2Flikecoin_de-portrait.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=like" width="485" height="212" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd30f89b074f3d1562e687e26ba280b8/href">https://medium.com/media/cd30f89b074f3d1562e687e26ba280b8/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c15d4e8c567a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/%E4%BB%80%E9%BA%BC%E6%98%AF%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91%E4%B8%AD%E7%9A%84%E5%B0%81%E8%A3%9D-%E7%B9%BC%E6%89%BF%E5%92%8C%E5%A4%9A%E5%9E%8B%E7%89%B9%E6%80%A7-c15d4e8c567a">什麼是物件導向中的封裝、繼承和多型特性？</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[什麼是類別 Class、物件 Object 和一個實體 Instance？差別是什麼？]]></title>
            <link>https://medium.com/w-learning-note/%E4%BB%80%E9%BA%BC%E6%98%AF%E9%A1%9E%E5%88%A5-class-%E7%89%A9%E4%BB%B6-object-%E5%92%8C%E4%B8%80%E5%80%8B%E5%AF%A6%E9%AB%94-instance-%E5%B7%AE%E5%88%A5%E6%98%AF%E4%BB%80%E9%BA%BC-831a7306672a?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/831a7306672a</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Sat, 03 Aug 2019 06:20:54 GMT</pubDate>
            <atom:updated>2020-12-21T15:48:15.894Z</atom:updated>
            <content:encoded><![CDATA[<p>類別Class類似物件的設計圖、範本，簡單來說類別只是定義物件形成時的樣子。</p><p>物件Object是由屬性(attributes)和方法(method)構成，有實體概念的東西，例如在現實生活中人、鳥、車子都可以稱為物件。</p><p>以鳥為例：</p><p>屬性(attributes)就是指毛的顏色、嘴巴的形狀等等</p><p>方法(method)則是指會飛、會吃等等</p><p>類別和物件有點像是建築設計圖和房子的關係，而實例Instance就是某個類別真實創造出來的物件。</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Frepl.it%2F%40Wumei%2F01%3Flite%3Dtrue&amp;display_name=repl.it&amp;url=https%3A%2F%2Frepl.it%2F%40Wumei%2F01%3Flite%3Dtrue&amp;image=https%3A%2F%2Frepl.it%2Fpublic%2Fimages%2Freplit-logo-800x600.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=repl" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/25be621732dffc82d9f650afc02b8a57/href">https://medium.com/media/25be621732dffc82d9f650afc02b8a57/href</a></iframe><p>參考：</p><p><a href="https://blog.miniasp.com/post/2009/08/27/OOP-Basis-What-is-class-and-object.aspx">物件導向基礎：何謂類別(Class)？何謂物件(Object)？</a></p><p><a href="http://teddy-chen-tw.blogspot.com/2012/01/2object-class-instance.html">什麼是物件導向（2）：Object, Class, Instance</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fbutton.like.co%2Fin%2Fembed%2Fwendyhj%2Fbutton&amp;display_name=LikeCoin&amp;url=https%3A%2F%2Fbutton.like.co%2Fwendyhj&amp;image=https%3A%2F%2Fstatic.like.co%2Flikecoin_de-portrait.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=like" width="485" height="212" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd30f89b074f3d1562e687e26ba280b8/href">https://medium.com/media/cd30f89b074f3d1562e687e26ba280b8/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=831a7306672a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/%E4%BB%80%E9%BA%BC%E6%98%AF%E9%A1%9E%E5%88%A5-class-%E7%89%A9%E4%BB%B6-object-%E5%92%8C%E4%B8%80%E5%80%8B%E5%AF%A6%E9%AB%94-instance-%E5%B7%AE%E5%88%A5%E6%98%AF%E4%BB%80%E9%BA%BC-831a7306672a">什麼是類別 Class、物件 Object 和一個實體 Instance？差別是什麼？</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode Exercise 5]]></title>
            <link>https://medium.com/w-learning-note/leetcode-exercise-5-7ddd8ffc645b?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/7ddd8ffc645b</guid>
            <category><![CDATA[中文]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Thu, 07 Feb 2019 16:33:00 GMT</pubDate>
            <atom:updated>2019-02-07T16:32:35.108Z</atom:updated>
            <content:encoded><![CDATA[<p>Unique Email Addresses</p><p><a href="https://leetcode.com/problems/unique-email-addresses/">Unique Email Addresses - LeetCode</a></p><h4>Description</h4><blockquote>Every email consists of a local name and a domain name, separated by the @ sign.</blockquote><blockquote>For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.</blockquote><blockquote>Besides lowercase letters, these emails may contain &#39;.&#39;s or &#39;+&#39;s.</blockquote><blockquote>If you add periods (&#39;.&#39;) between some characters in the <strong>local name</strong> part of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example, &quot;alice.z@leetcode.com&quot; and &quot;alicez@leetcode.com&quot;forward to the same email address. (Note that this rule does not apply for domain names.)</blockquote><blockquote>If you add a plus (&#39;+&#39;) in the <strong>local name</strong>, everything after the first plus sign will be <strong>ignored</strong>. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.)</blockquote><blockquote>It is possible to use both of these rules at the same time.</blockquote><blockquote>Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?</blockquote><p>看起來有點長，大意是說email是由local name和domain name組成，中間是由@來區隔，例如alice@leetcode.com<em>，</em>alice 是local name，leetcode.com 則是domain name。</p><p>除此之外，email是可以包含&#39;.&#39;或是&#39;+&#39;。</p><p>如果在local name加入&#39;.&#39; ，mail會傳送到沒有&#39;.&#39; 的地址，例如&quot;alice.z@leetcode.com&quot; 和&quot;alicez@leetcode.com&quot; 是傳送到相同的地址。(這項規則不適用於domain name)</p><p>如果在local name加入&#39;+&#39; ，任何在第一個加號之後的內容會被忽略，例如m.y+name@email.com 會被傳送到my@email.com。(同樣地，這項規則不適用於domain name)</p><p>這兩個規則是能同時使用的。</p><p>當題目給了一組email的清單時，要回傳實際上有幾個不同地址的email。</p><h4>Example</h4><pre>#1<br><strong>Input:<br></strong>[&quot;test.email+alex@leetcode.com&quot;,&quot;test.e.mail+bob.cathy@leetcode.com&quot;,&quot;testemail+david@lee.tcode.com&quot;]</pre><pre><strong>Output:</strong> 2</pre><pre><strong>Explanation:</strong> <br>&quot;testemail@leetcode.com&quot; and &quot;testemail@lee.tcode.com&quot; actually receive mails</pre><h4>構思</h4><ol><li>先個別處理每一個email，按照規則簡化</li><li>每個email先拆分local name和domain name，只針對local name的部分處理，先處理&#39;+&#39;，去掉後面的內容，再將所有&#39;.&#39; 刪除</li><li>全部處理完後再去除掉重複的地址</li><li>計算總數</li></ol><h4>實作程式碼</h4><pre># <a href="http://twitter.com/param">@param</a> {String[]} emails<br># <a href="http://twitter.com/return">@return</a> {Integer}<br>def num_unique_emails(emails)<br>  emails = emails.map do |email|<br>    email = email.split(&quot;@&quot;)<br>    local = email[0].split(&quot;+&quot;)<br>    local = local[0].delete(&quot;.&quot;)<br>    email = local + &quot;@&quot; + email[1]<br>  end<br>  emails.uniq.size     <br>end</pre><p><a href="https://ruby-doc.com/core-2.5.0/String.html#method-i-split"><strong>split</strong></a><strong>:</strong></p><p>會將string按照設定的條件，拆換成array，在這邊的用法就是用”@”將email address分成兩邊：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dfgImXKGGdlGn05Iykaqlg.png" /><figcaption>split</figcaption></figure><p><a href="https://ruby-doc.com/core-2.5.0/Array.html#method-i-uniq"><strong>uniq</strong></a><strong>:</strong></p><p>回傳一個去掉重複值的Array</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*6ttQwfUl_1rm3IxdJOH4Pg.png" /><figcaption>uniq</figcaption></figure><h4>結果</h4><p>beats 94.29%</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ADgYP7K3Fe91QMOmERtHkw.png" /></figure><h4>Runtime最短的寫法: 60ms</h4><pre><strong>def</strong> <strong>num_unique_emails</strong>(emails)     <br>  uniques = 0     <br>  ht = Hash.new(0)     <br>  emails.each <strong>do</strong> |e|         <br>    at_position = e.index(&#39;@&#39;)                  <br>   <br>    <strong>if</strong> e.<strong>include</strong>? &#39;+&#39;             <br>      local = e[0,e.index(&#39;+&#39;)]         <br>    <strong>else</strong>             <br>      local = e[0,at_position]         <br>    <strong>end</strong>                  </pre><pre>    domain = e[at_position..-1]         <br>    local.gsub!(&#39;.&#39;,&#39;&#39;)                  </pre><pre>    canonical = local &lt;&lt; domain                  <br>    uniques+=1 <strong>if</strong> ht[canonical]==0          <br>    ht[canonical]+=1     <br>  <strong>end</strong>    <br>  uniques <br><strong>end</strong></pre><p>一樣是對每個email個別處理，先用index找出&#39;@&#39;的位置，再判斷是否有&#39;+&#39;，有的話local就是開頭到&#39;+&#39;的位置，沒有的話local就是開頭到&#39;@&#39;之前的位置，再將local的&#39;.&#39;用gsub!代換成空的，而domain則是從&#39;@&#39;到結尾。</p><p>最後將local和domain合在一起變成完整的email地址存入canonical這個變數，這時判斷ht這個hash中key為canonical時，value是否等於0(因為初始化hash的時候給了default value 0，所以若canonical為空時會回傳0)，等於0的話會將uniques的值加1，並且在ht中在key等於canonical的value加上1，這樣下次遇到一樣的canonical時，uniques的值便不會改變，最後再回傳uniques即是答案。</p><p><a href="https://ruby-doc.com/core-2.5.0/String.html#method-i-index"><strong>index</strong></a>:</p><p>回傳在字串中第一個遇見給定的子字串或字串樣板(Regexp)的索引</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*CkPvvFJHv5o3r0105hvpMw.png" /><figcaption>index</figcaption></figure><p><a href="https://ruby-doc.com/core-2.5.0/String.html#method-i-gsub-21"><strong>gsub!</strong></a>:</p><p>gsub(pattern, replacement)會回傳一個copy，用後面的參數取代掉字串中所有出現的第一個參數，而加上&#39;!&#39;則表示會直接將原字串代換掉。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*oaWKHlVzODGMoKnDaLHgLg.png" /><figcaption>gsub</figcaption></figure><p><a href="https://ruby-doc.com/core-2.5.0/String.html#method-i-3C-3C"><strong>&lt;&lt;</strong></a>:</p><p>在string後添加給的物件，如果是數字的話，會被認為是codepoint。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*-SnZQSOv4MfU3LAbPWM6oA.png" /><figcaption>&lt;&lt;</figcaption></figure><p>關於hash部分的操作，可以參考下面的範例</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*lCKtZ6e7i8vPKyrDxHrW8w.png" /><figcaption>hash</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ddd8ffc645b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/leetcode-exercise-5-7ddd8ffc645b">Leetcode Exercise 5</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode Exercise 4]]></title>
            <link>https://medium.com/w-learning-note/leetcode-exercise-4-3a41450bdda5?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/3a41450bdda5</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Thu, 29 Nov 2018 07:30:13 GMT</pubDate>
            <atom:updated>2018-11-29T07:30:02.622Z</atom:updated>
            <content:encoded><![CDATA[<h4>Jewels and Stones</h4><p><a href="https://leetcode.com/problems/jewels-and-stones/">Jewels and Stones - LeetCode</a></p><h4>Description</h4><blockquote>You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.</blockquote><blockquote>The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so &quot;a&quot; is considered a different type of stone from &quot;A&quot;.</blockquote><p>字串J中的字代表是珠寶的石頭種類，字串S中每一個字代表你有的石頭，你想知道你有的石頭裡面，有幾個珠寶。</p><p>字串J中所有的字都不同，且J和S中都只有字母，大小寫視為不同。</p><h4>Example</h4><pre>#1<br><strong>Input:</strong> J = &quot;aA&quot;, S = &quot;aAAbbbb&quot;<br><strong>Output:</strong> 3</pre><pre>#2<br><strong>Input:</strong> J = &quot;z&quot;, S = &quot;ZZ&quot;<br><strong>Output:</strong> 0</pre><h4>構思</h4><ol><li>將將J字串的字一個一個比對S有幾個</li><li>將所有數字相加</li></ol><h4>實作程式碼</h4><pre># <a href="http://twitter.com/param">@param</a> {String} j<br># <a href="http://twitter.com/param">@param</a> {String} s<br># <a href="http://twitter.com/return">@return</a> {Integer}<br>def num_jewels_in_stones(j, s)<br>  ans = 0<br>  j.each_char { |char| <br>    ans += s.count(char) <br>  }<br>  return ans    <br>end</pre><h4>結果</h4><p>beats 92.72%</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3TDdar5CUHZ7pJasHEH5yA.png" /></figure><h4>Runtime最短的寫法: 44 ms</h4><pre><strong>def</strong> <strong>num_jewels_in_stones</strong>(j, s)     <br>  s.count(j)<br><strong>end</strong></pre><p>嗯……看起來其實用一個count就可以解決了，</p><p>那就來看看<a href="http://ruby-doc.org/core-2.1.2/String.html#method-i-count">count</a>這個method的用法吧，直接看下面的結果：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HZzsilVE_QX4cQtlo8p35w.png" /><figcaption>count</figcaption></figure><p>可以看到，不管用”A”還是”AA”，在”aAAbbbb”中得到的結果都是2，</p><p>原因是count計算的是字母(<em>characters</em>)，不是字串，所以不管是AA還是A，結果都是一樣的。</p><p>那如果我是想算”AA”的數量呢？可以參考這篇<a href="https://stackoverflow.com/questions/25938430/ruby-how-to-count-the-number-of-times-a-string-appears-in-another-string/25938518#25938518">stackoverflow</a>。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a41450bdda5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/leetcode-exercise-4-3a41450bdda5">Leetcode Exercise 4</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby on Rails 如何實現 MVC？]]></title>
            <link>https://medium.com/w-learning-note/ruby-on-rails-%E5%A6%82%E4%BD%95%E5%AF%A6%E7%8F%BE-mvc-fbf5d29e0725?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/fbf5d29e0725</guid>
            <category><![CDATA[中文]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[mvc]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Sun, 25 Nov 2018 17:28:48 GMT</pubDate>
            <atom:updated>2018-11-25T17:31:17.229Z</atom:updated>
            <content:encoded><![CDATA[<h4>Note</h4><h4>MVC是什麼？</h4><p>聊到Ruby on Rails如何實現MVC，就要先來了解MVC是什麼？</p><p><a href="https://zh.wikipedia.org/wiki/MVC">MVC</a>是一種軟體架構模式，把軟體分為三個部分：模型（Model）、視圖（View）和控制器（Controller）。每個部分有各自的職責，能讓程式的架構能更加直覺，也因此更容易進行分工。</p><ul><li>模型Model：用於封裝與應用程式的商業邏輯(Business logic)相關的資料以及對資料的處理方法，處理資料庫的操作。</li><li>視圖View：負責將資料傳遞給使用者，並處理畫面的呈現。</li><li>控制器Controller：負責接收從外部 (使用者介面) 傳來的訊息並作出回應，指派工作，是 Model 和 View 的中間人，起到不同層面間的組織作用。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*jL1SZkdofAa9jQTJrkTB3A.png" /><figcaption>MVC form wiki</figcaption></figure><h4>Ruby on Rails如何實現MVC?</h4><ul><li>Model (<a href="https://rails.ruby.tw/active_record_basics.html">Active Record</a>)</li></ul><p>Active Record是Rails中提供<a href="https://zh.wikipedia.org/wiki/%E5%AF%B9%E8%B1%A1%E5%85%B3%E7%B3%BB%E6%98%A0%E5%B0%84">ORM</a>（Object-Relational Mapping：將應用程式中複雜的物件，對應到關聯式資料庫管理系統中的資料表）的元件，負責新增與操作需要持久存在資料庫裡的資料。</p><ul><li>View (<a href="https://rails.ruby.tw/action_view_overview.html">Action View</a>)</li></ul><p>Action View 與 Action Controller 是 Action Pack 中的兩個主要元件。 在 Rails 裡，網路請求是由 Action Pack 負責處理的。通常 Action Controller 是與資料庫溝通，根據需求來執行 CRUD 操作。而 Action View 則接著負責編譯出回應。</p><p>Action View 模版(templates)是由嵌入 HTML 的 Ruby 撰寫而成(Embedded Ruby, erb)，為了避免模版充斥混亂的程式碼，Action View 提供了許多輔助方法(helper)，用來撰寫表單、日期及字串等。每個 Controller 在 app/views 中都會有一個對應的資料夾，裡面包含了該 Controller 的模板檔案(template files)。這些檔案用來顯示 Controller 各個動作的結果頁面。</p><p>回傳給用戶端的完整 HTML 是由這個 ERB 檔案(Templates)、版型(Layouts)，以及其它引用的局部頁面(Partials)組成。</p><ul><li>Controller (<a href="https://rails.ruby.tw/action_controller_overview.html">Action Controller</a>)</li></ul><p>一個請求進來，路由(router)決定是哪個 Controller 的工作後，便把工作指派給該Controller，Controller 負責處理該請求，給出適當的回應。幸運的是，Action Controller 把大部分的苦差事都辦好了，只需遵循一些簡單的規範來寫程式。</p><p>對多數按照 <a href="http://en.wikipedia.org/wiki/Representational_state_transfer">REST</a> 規範來編寫的應用程式來說，Controller 的工作便是接收請求（開發者看不到），去 Model 讀或寫資料，再使用 View 來產生出 HTML，Controller 因此可以想成是 Model 與 View 的中間人，負責替 Model 將資料傳給 View，讓 View 可以顯示資料給使用者。Controller 也將使用者更新或儲存的資料，存回 Model。</p><p>關於Rails MVC的流程圖解與實際應用以及目錄結構，可以參考高見龍老師的<a href="https://railsbook.tw/chapters/10-mvc.html#mvc-flow">Model、View、Controller 三分天下</a></p><p>參考：</p><ul><li><a href="https://zh.wikipedia.org/wiki/MVC">MVC</a></li><li><a href="https://rails.ruby.tw/">Ruby on Rails 指南</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbf5d29e0725" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/ruby-on-rails-%E5%A6%82%E4%BD%95%E5%AF%A6%E7%8F%BE-mvc-fbf5d29e0725">Ruby on Rails 如何實現 MVC？</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby 和 Ruby on Rails 的關係是什麼？]]></title>
            <link>https://medium.com/w-learning-note/ruby-on-rails%E5%9F%BA%E6%9C%AC%E8%A7%80%E5%BF%B5%E7%AD%86%E8%A8%98-5b282cdd7de5?source=rss----b57d554777d2---4</link>
            <guid isPermaLink="false">https://medium.com/p/5b282cdd7de5</guid>
            <category><![CDATA[中文]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Wendy Wu]]></dc:creator>
            <pubDate>Fri, 23 Nov 2018 07:45:33 GMT</pubDate>
            <atom:updated>2018-11-25T08:20:54.674Z</atom:updated>
            <content:encoded><![CDATA[<p>Rails 是一個使用 Ruby 開發的 Web 框架。</p><p>這個框架把Ｗeb開發的過程需要的細節、常用的模組或函式庫處理好，讓開發 Web 應用程式變成一件簡單的事情。</p><p>在Rails開發中有兩個主要的原則：</p><ul><li>慣例優於設定(Convention over Configuration, CoC）</li></ul><p>「慣例」就像是不成文的規定，在 Rails 裡有相當多這樣的慣例，例如像是專案的目錄結構、資料表的關連及命名等，Rails 不希望你浪費太多時間無止境的配置設定上。</p><p><a href="https://zh.wikipedia.org/wiki/%E7%BA%A6%E5%AE%9A%E4%BC%98%E4%BA%8E%E9%85%8D%E7%BD%AE">wiki</a>的舉例提到：</p><blockquote>如果模型中有個名為Sale的類，那麼資料庫中對應的表就會默認命名為sales。只有在偏離這一約定時，例如將該表命名為”products_sold”，才需寫有關這個名字的配置。</blockquote><ul><li>不要做重複的事（Don’t Repeat Yourself, DRY）</li></ul><p>這也是物件導向程式中的基本原則，在軟體開發中，避免一再重複的資訊，所寫的程式才容易維護。如果有程式碼不斷重複出現，就應該把重複的部份抽離出來，整理成為一個方法、類別或模組。</p><p>參考：</p><ul><li><a href="https://rails.ruby.tw/getting_started.html">Rails 起步走 - Ruby on Rails 指南</a></li><li><a href="https://railsbook.tw/chapters/01-ecosystem-and-introduction.html#about-rails">生態圈及簡介 | 高見龍</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b282cdd7de5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/w-learning-note/ruby-on-rails%E5%9F%BA%E6%9C%AC%E8%A7%80%E5%BF%B5%E7%AD%86%E8%A8%98-5b282cdd7de5">Ruby 和 Ruby on Rails 的關係是什麼？</a> was originally published in <a href="https://medium.com/w-learning-note">W-Learning Note</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>