<?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[Stories by Phil Lee on Medium]]></title>
        <description><![CDATA[Stories by Phil Lee on Medium]]></description>
        <link>https://medium.com/@prodigy7748?source=rss-c9ff2e4a7b25------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Phil Lee on Medium</title>
            <link>https://medium.com/@prodigy7748?source=rss-c9ff2e4a7b25------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:54:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@prodigy7748/feed" 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 MVC 架構解析]]></title>
            <link>https://prodigy7748.medium.com/ruby-mvc-%E6%9E%B6%E6%A7%8B%E8%A7%A3%E6%9E%90-567e83753720?source=rss-c9ff2e4a7b25------2</link>
            <guid isPermaLink="false">https://medium.com/p/567e83753720</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Phil Lee]]></dc:creator>
            <pubDate>Thu, 06 Jul 2023 03:35:34 GMT</pubDate>
            <atom:updated>2023-07-06T03:35:34.902Z</atom:updated>
            <content:encoded><![CDATA[<p>MVC，全名為 Model-View-Controller，是一種軟體設計模式，用於組織代碼以便於維護和擴展。在 Ruby 中，我們通常會在 Rails 框架中看到 MVC 的架構。</p><h3>MVC 架構簡介</h3><ul><li>Model（模型）：模型負責管理應用程式的數據和業務邏輯。它與數據庫直接交互，進行數據的讀取和寫入操作。</li><li>View（視圖）：視圖是用戶看到和與之互動的界面。它通常是 HTML、CSS 和 JavaScript 的組合，用於顯示和操作數據。</li><li>Controller（控制器）：控制器是模型和視圖之間的橋樑。它接收用戶的輸入，通過模型操作數據，並將結果呈現給視圖。</li></ul><h3>Ruby on Rails 中的 MVC</h3><p>Ruby on Rails 是一個 MVC 框架，讓我們以一個簡單的部落格應用程式為例，來看看 MVC 在 Rails 中是如何工作的。</p><h3>Model</h3><p>在 Rails 中，我們可以使用 ActiveRecord 來創建和管理模型。例如，我們可能有一個 Post 模型來管理部落格文章：</p><pre>class Post &lt; ApplicationRecord<br>  validates :title, presence: true<br>  validates :body, presence: true<br>end</pre><p>這個 Post 模型將與數據庫中的 posts 表格相對應，並且我們可以通過模型來進行數據的讀取和寫入。</p><h3>View</h3><p>視圖是用來顯示數據的。在 Rails 中，視圖通常是 ERB（Embedded Ruby）模板。例如，我們可能有一個視圖來顯示所有的部落格文章：</p><pre>&lt;h1&gt;My Blog&lt;/h1&gt;<br><br>&lt;% @posts.each do |post| %&gt;<br>  &lt;h2&gt;&lt;%= post.title %&gt;&lt;/h2&gt;<br>  &lt;p&gt;&lt;%= post.body %&gt;&lt;/p&gt;<br>&lt;% end %&gt;</pre><p>這個視圖將會遍歷 @posts 變量中的所有文章，並將它們的標題和內容顯示出來。</p><h3>Controller</h3><p>控制器負責處理用戶的請求並返回響應。在 Rails 中，我們可以創建一個控制器來管理部落格文章的 CRUD（創建、讀取、更新、刪除）操作。例如：</p><pre>class PostsController &lt; ApplicationController<br>  def index<br>    @posts = Post.all<br>  end<br><br>  def show<br>    @post = Post.find(params[:id])<br>  end<br><br>  def new<br>    @post = Post.new<br>  end<br><br>  def create<br>    @post = Post.new(post_params)<br>    if @post.save<br>      redirect_to @post<br>    else<br>      render &#39;new&#39;<br>    end<br>  end<br><br>  private<br><br>  def post_params<br>    params.require(:post).permit(:title, :body)<br>  end<br>end</pre><p>這個 PostsController 包含了多個動作，如 index（顯示所有文章）、show（顯示單一文章）、new（創建新文章的表單）和 create（保存新文章）。每個動作都會與一個或多個View相對應。</p><p>這就是Ruby的MVC架構的運作方式。通過將應用程式分為模型、視圖和控制器，我們可以將關注點分離，使得代碼更容易理解和維護。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=567e83753720" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby 中的 size 與 count 方法：深度解析及使用案例]]></title>
            <link>https://prodigy7748.medium.com/ruby-%E4%B8%AD%E7%9A%84-size-%E8%88%87-count-%E6%96%B9%E6%B3%95-%E6%B7%B1%E5%BA%A6%E8%A7%A3%E6%9E%90%E5%8F%8A%E4%BD%BF%E7%94%A8%E6%A1%88%E4%BE%8B-a64b5d3c3a30?source=rss-c9ff2e4a7b25------2</link>
            <guid isPermaLink="false">https://medium.com/p/a64b5d3c3a30</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Phil Lee]]></dc:creator>
            <pubDate>Thu, 06 Jul 2023 03:17:32 GMT</pubDate>
            <atom:updated>2023-07-06T03:17:32.461Z</atom:updated>
            <content:encoded><![CDATA[<p>Ruby 是一種物件導向、動態型別且具有優雅語法的程式語言。在 Ruby 中，我們經常需要對數據結構如陣列或哈希進行操作，而 size 和 count 是兩種常見的方法。本篇文章將深入探討這兩種方法的作用和差異，以及分析它們的優缺點。</p><h3>size 方法</h3><p>size 方法在 Ruby 中廣泛應用於多種數據結構，包括陣列、Hash、字串等。這個方法的主要作用是返回數據結構的元素數量。</p><p>例如，如果我們有一個陣列 array = [1, 2, 3, 4, 5]，我們可以用 array.size 來獲得陣列的元素數量，這將返回 5。</p><p>size 方法的一個主要優點是其效率。在內部實現上，size 方法直接返回存儲的元素數量，所以操作速度非常快。然而，它的缺點是不能針對特定條件進行計數。</p><h3>count 方法</h3><p>與 size 方法不同，count 方法在 Ruby 中具有更多的靈活性。你可以用它來獲得數據結構的元素數量，就像 size 方法一樣，但你也可以用它來計算滿足特定條件的元素數量。</p><p>例如，如果你有一個陣列 array = [1, 2, 3, 4, 5]，你可以用 array.count 來獲得元素數量，這將返回 5。但你也可以給 count 方法傳遞一個塊，來計算滿足特定條件的元素數量。例如，array.count { |x| x &gt; 3 } 將返回 2，因為只有兩個元素（4 和 5）大於 3。</p><p>count 方法的優點是它的靈活性。它可以計數整個數據結構，也可以計數滿足特定條件的元素。然而，它的缺點是效率較低。當你給 count 方法傳遞一個塊時，它必須對數據結構的每一個元素進行迭代並運行塊，這可能在大型數據結構上非常耗時。</p><h3>比較 size 和 count 方法</h3><p>總的來說，size 方法和 count 方法都是非常有用的工具，選擇使用哪一個主要取決於你的需求。</p><p>如果你只需要快速獲得數據結構的元素數量，size 方法將是最好的選擇，因為它的效率最高。然而，如果你需要計算滿足特定條件的元素數量，那麼 count 方法將是你的首選，因為它提供了更多的靈活性。</p><p>以下是一個具體的例子來說明兩者的差異：</p><pre>array = (1..1_000_000).to_a<br><br># 使用 size 方法<br>start_time = Time.now<br>array.size<br>end_time = Time.now<br>puts &quot;Time taken with size: #{end_time - start_time} seconds.&quot;<br><br># 使用 count 方法<br>start_time = Time.now<br>array.count { |x| x &gt; 500_000 }<br>end_time = Time.now<br>puts &quot;Time taken with count: #{end_time - start_time} seconds.&quot;<br></pre><p>在上述代碼中，我們有一個包含一百萬個元素的陣列。我們首先使用 size 方法來獲取元素數量，然後使用 count 方法來計算大於 500,000 的元素數量。你會發現 count 方法需要的時間遠遠超過 size 方法。</p><h3>結論</h3><p>Ruby 的 size 和 count 方法都是強大的工具，但它們適用於不同的情況。當你需要快速獲得元素數量時，size 方法是最好的選擇；當你需要計算滿足特定條件的元素數量時，count 方法將是更好的選擇。在實際應用中，我們需要根據具體需求來選擇最適合的方法。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a64b5d3c3a30" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby 學習筆記-模組(Module)]]></title>
            <link>https://prodigy7748.medium.com/ruby-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E6%A8%A1%E7%B5%84-module-c01088c7f81a?source=rss-c9ff2e4a7b25------2</link>
            <guid isPermaLink="false">https://medium.com/p/c01088c7f81a</guid>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Phil Lee]]></dc:creator>
            <pubDate>Wed, 25 Nov 2020 03:23:46 GMT</pubDate>
            <atom:updated>2020-11-25T03:27:09.345Z</atom:updated>
            <content:encoded><![CDATA[<h3>有時有多個類別都需要使用同個方法，這時候可以定義一個模組，讓每個類別可以引入取用。</h3><h3>模組與繼承的不同之處：</h3><ul><li>模組沒有繼承的功能：</li></ul><pre>module Flyable &lt; other module<br>end</pre><pre>#無法繼承</pre><ul><li>模組不能被實體化：</li></ul><pre>module Flyable<br>end</pre><pre>Dog = Flyable.new </pre><pre>#undefined method `new&#39; for Flyable:Module (NoMethodErro</pre><h3>模組引入有兩種寫法：</h3><ol><li>在類別內include模組，此類別製造的實體可使用模組的方法（實體方法）</li></ol><pre>module Invisible<br> def woolala<br>  puts &quot;nobody can see #{@name} now!&quot;<br> end<br>end</pre><pre>class Human<br>  include Invisible<br>  def initialize(name)<br>    @name = name<br>  end<br>end</pre><pre>Phil = Human.new(&quot;Phil&quot;)<br>Phil.woolala</pre><pre>#印出 nobody can see Phil now!</pre><p>2. 在類別內extend模組，此類別可直接使用方法（類別方法），但其製造的 實體不可使用此方法</p><pre>module Flyable<br>  def fly<br>    puts &#39;I can Fly!&#39;<br>  end<br>end</pre><pre>class Cat<br>	extend Flyable<br>end</pre><pre>Cat.fly<br>#印出 I can Fly!</pre><pre>Phil = Cat.new<br>Phil.fly<br>#undefined method `fly&#39; for #&lt;Cat:0x0000563c17581b38&gt; (NoMethodError)</pre><h3>如有重複的類別名稱，可用不同模組區隔，製作實體時，需用” :: “來鎖定欲呼叫的類別。</h3><pre>module A<br>  module Z<br>    class Dog<br>    end<br>  end<br>end</pre><pre>module B<br>  class Dog<br>  end<br>end</pre><pre>Ryan = A::Z::Dog.new #必須一層層呼叫到類別<br>Will = B::Dog.new</pre><p>冒號的位置在Ruby代表不同意思</p><ul><li>冒號在左邊，代表符號</li></ul><pre>has_name :name</pre><ul><li>冒號在右邊，代表hash裡面的key</li></ul><pre>{name: &quot;Phil&quot;, age: 32}</pre><ul><li>冒號在中間，前者是key，後者是符號，中間用空格隔開</li></ul><pre>{direction: :up, power: 100}</pre><ul><li>冒號在中間，而且沒有空格，代表呼叫模組裡面的類別</li></ul><pre>class User &lt; ActiveRecord::Base #User繼承ActiveRecord模組裡面的Base類別<br>end</pre><p>參考資料：</p><ul><li><a href="https://railsbook.tw/chapters/08-ruby-basic-4.html">類別（Class）與模組（Module） | 高見龍</a></li><li><a href="https://tingtinghsu.github.io/blog/articles/2018-09-11-day03_ruby_interview_questions_include_vs_extend">Ruby面試精選30題 - Day03 Module模組: include與extend · Ting&#39;s Square</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c01088c7f81a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby 學習筆記-Symbol（符號）]]></title>
            <link>https://prodigy7748.medium.com/ruby-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-symbol-%E7%AC%A6%E8%99%9F-f8f9b96921c4?source=rss-c9ff2e4a7b25------2</link>
            <guid isPermaLink="false">https://medium.com/p/f8f9b96921c4</guid>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Phil Lee]]></dc:creator>
            <pubDate>Mon, 05 Oct 2020 06:56:53 GMT</pubDate>
            <atom:updated>2020-10-05T06:56:53.019Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Symbol是什麼？</strong></h3><p>在ruby的撰寫過程中，時常看到:name, :title等前面帶有冒號的用法，它就是symbol，中譯為符號。</p><p>Ruby在處理每一個物件時，會在記憶體中產生一組新的object_id，所以每一個東西都不相同，但Symbol 即使被重複使用，也都是同一個物件，object_id 不會變，不會消耗多餘的記憶體。</p><p>ruby官方api對symbol下的註解及example：</p><blockquote><a href="https://ruby-doc.org/core-2.7.2/Symbol.html">Symbol</a> objects represent names inside the Ruby interpreter. They are generated using the :name and :&quot;string&quot; literals syntax, and by the variousto_sym methods. The same <a href="https://ruby-doc.org/core-2.7.2/Symbol.html">Symbol</a> object will be created for a given name or string for the duration of a program&#39;s execution, regardless of the context or meaning of that name. Thus if Fred is a constant in one context, a method in another, and a class in a third, the <a href="https://ruby-doc.org/core-2.7.2/Symbol.html">Symbol</a> :Fred will be the same object in all three contexts.</blockquote><blockquote>假設 Fred 在一個 context 中是一個constant(常數)，在一個 context中是method(方法)，同時是另一個 context 的class(類別)， Symbol：Fred 全都是相同的物件，會得到同個object_id。</blockquote><pre>module One<br>  class Fred<br>  end<br>  $f1 = :Fred<br>end<br>module Two<br>  Fred = 1<br>  $f2 = :Fred<br>end<br>def Fred()<br>end</pre><pre><em>$f3 = :Fred$f1.object_id  </em><strong><em>      </em></strong><em># 2514190<br>$f2.object_id        # 2514190<br>$f3.object_id        # 2514190</em></pre><h3>與Variable(變數)有何不同？</h3><p>ruby是個物件導向的程式語言，每一個變數、數字、文字都是物件，symbol也不例外，可將它看作是「帶有名字的物件」，本質上就是「值」，<em>不需要也不能指向任何東西</em>，變數則是指向某物件的名字，無法單獨存在。而且你也沒辦法直接拿 Symbol 來當變數，像這樣會出現語法錯誤:</p><p>:name = “Phil Lee”=&gt;#出現錯誤碼：syntax error, unexpected ‘=’, expecting end-of-input<br>:name = “Phil Lee”</p><h3><strong>與String(字串)有何不同？</strong></h3><h4>1. string的內容可以變，symbol不行</h4><p>相似之處在於，他們都可以使用字元方法如.length及.upcase等，也都可以使用中括號 + 索引來取得其中某個字元，但如果想修改symbol的內容，會發生語法錯誤，<em>因為 Symbol 類別並沒有 []= 這個方法。</em></p><h4>2. symbol的提取效率比string好</h4><p>上面提到，相同的string對ruby來說是不同物件，而相同的symbol則對應到記憶體中的相同物件。不同symbol 在做比較的時候，是直接比對這兩顆物件的 object_id是否相同，而字串比較則是一個字母一個字母逐一比對。所以在效率上來說，string在做比較的時間複雜度會隨著字母的數量而增加，但 Symbol 因為只比較是不是同一個物件，所以它的複雜度是不變的。</p><h4>3. string跟symbol間可以互相轉換</h4><p>string可透過.to_sym及.intern轉換成symbol。</p><p>symbol則可透過.to_s及id2name轉換成string。</p><h4>4. 如果把 String 給”冰凍”(freeze)起來，它便不可修改，object_id 也會是同樣的。</h4><pre><strong>3.</strong>times do   <br>  puts &quot;hello&quot;<strong>.</strong>freeze<strong>.</strong>object_id <br>end  <br><em># =&gt; 70314415546380</em> <br><em># =&gt; 70314415546380</em> <br><em># =&gt; 70314415546380</em></pre><h3>Symbol的使用時機？</h3><blockquote>Strings are used to work with <strong>data</strong>.</blockquote><blockquote>Symbols are <strong>identifiers</strong>.</blockquote><blockquote>You should use symbols as <strong>names</strong> or labels for things (like methods) &amp; use <a href="https://www.rubyguides.com/2018/01/ruby-string-methods/">strings</a> when you care more about the <strong>data</strong> (individual characters).</blockquote><p>當我們需要內容是不可變動時，可使用symbol，因為 Symbol具有不可變(immutable)，像標籤一樣的特性，以及它的查找、比較的速度比字串還快，它很適合用來當 Hash 的 Key。</p><p><strong>hash = {a: 1, b: 2, c: 3}</strong></p><p>另外有的方法的參數是用字串，有的是用 Symbol，有的是兩種都能用，那該怎麼知道該用哪一種? 可<a href="https://ruby-doc.org/core-2.7.2/">參考ruby官方api</a>。</p><p>參考資料：</p><ul><li><a href="https://ruby-doc.org/core-2.7.2/">Ruby 2.7.2</a></li><li><a href="https://kaochenlong.com/2016/04/25/string-and-symbol/">Ruby 語法放大鏡之「有的變數變前面有一個冒號(例如 :name)，是什麼意思?」 | 高見龍</a></li><li><a href="https://www.rubyguides.com/2018/02/ruby-symbols/">What Are Ruby Symbols &amp; How Do They Work? - RubyGuides</a></li><li><a href="https://pjchender.github.io/2017/09/26/ruby-symbol%EF%BC%88%E7%AC%A6%E8%99%9F%EF%BC%89/">[Ruby] Symbol（符號）</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f8f9b96921c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>