<?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 Harry Phung on Medium]]></title>
        <description><![CDATA[Stories by Harry Phung on Medium]]></description>
        <link>https://medium.com/@huy.phung.sw?source=rss-32b96c6f85c6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*e02YyhSRRjuporOWB5vwkQ.jpeg</url>
            <title>Stories by Harry Phung on Medium</title>
            <link>https://medium.com/@huy.phung.sw?source=rss-32b96c6f85c6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 03:18:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@huy.phung.sw/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[Short Fiction 1]]></title>
            <link>https://medium.com/@huy.phung.sw/short-fiction-1-622e473e5c2f?source=rss-32b96c6f85c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/622e473e5c2f</guid>
            <category><![CDATA[saigon]]></category>
            <category><![CDATA[fiction]]></category>
            <category><![CDATA[literature]]></category>
            <category><![CDATA[vietnam]]></category>
            <category><![CDATA[storytelling]]></category>
            <dc:creator><![CDATA[Harry Phung]]></dc:creator>
            <pubDate>Tue, 06 Jun 2023 04:06:21 GMT</pubDate>
            <atom:updated>2023-06-06T04:08:48.224Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>If you are Vietnamese, you could read the Vietnamese version <a href="https://medium.com/@huy.phung.sw/t%E1%BB%B1-truy%E1%BB%87n-h%C6%B0-c%E1%BA%A5u-ef84203a2857">here</a>.</blockquote><p>The sunset, the sun slowly sinks behind the mountains surrounding the town of Aspen, Colorado, USA. Snow covered the streets and along the slopes, dotted here and there are black or brown spots of pine trees. Looking far across the hill opposite is a small villa. This season, snow covered the trees in the garden.</p><p>Upstairs, a few windows shed warm yellow light. In one corner window, the shadow of a man sitting bent over, perhaps working, perhaps reading a book. An ideal place to read books and watch the Christmas town.</p><p>The second floor room is not too big. In the corner of the room, there are two large bookcases, each nearly two meters high and about one meter wide. The bookcase closest to the corner is filled with books on computer science. Glancing over the book titles, we recognize a few on Programming Languages, one on Databases, and many more. The remaining shelf, closer to the desk by the window, is a much more interesting bookcase. Vaguely, we recognize The Godfather, translated by Ngoc Thu Lan. Next to it is Life of Pi, translated by Trinh Lu. The two books are quite old, perhaps brought from Vietnam. Peeping on the shelf are Sherlock Holmes by Conan Doyle, some other stories by Agatha Christie, Edogawa Ranpo and many other detective stories.</p><p>Opposite the two bookcases is a rather large display cabinet. The bottom compartment contains some neatly arranged Lego sets. The middle and top compartments are stacked with a variety of mechanical keyboards. There are plastic ones, aluminum ones, transparent ones, and wooden ones.</p><p>On top of the display cabinet is a large picture frame. On the right edge of the picture are two golden dogs, one brown and one white, standing on two legs, leaning slightly to the left to look at a couple sitting in the middle of the frame. In the middle is a young man between the ages of 25 and 30. Although Asian, he has fair skin and a handsome appearance. His hair is swept back, neatly styled, a very popular hairstyle of the 1980s. His eyes look straight down at his hands that are holding the girl’s hands next to him, his mouth smiles brightly. The girl sitting next to him has skin as fair as his, notably her short shoulder-length hairstyle looks very elegant. The girl stands out in the middle of the picture in a white dress, her eyes closed but seeming to smile, her mouth showing a happy smile. Although the frame is slightly faded over time, it is kept very clean, contrary to the thin layer of dust around. Next to the picture frame is a small wooden box, slightly dusty, opening the box, we can see many different fountain pens, such as Kaweco, Lamy or TWSBI. Next to the wooden box are many bottles of fountain pen ink in all colors, but blue ink is the most.</p><p>In the middle of the room, next to the window is a two meter long desk. The desk is wide but not cluttered, items such as speakers, keyboard and mouse are all wireless. The large screen is mounted on a very neat arm. Opposite the screen, the man is diligently typing, his hands trembling but still very smooth in every movement. The gaunt face has many wrinkles on the eyelids and cheeks. Although age spots have appeared, the fair skin still highlights his bright face. If you look closely, you can easily recognize that this is the young man in the picture, to be more precise, the young man in the picture is the youth of the old man in the room. Typing fast, occasionally the old man opens a notebook on the desk to scribble something. It’s past ten o’clock at night, but the old man is still sitting in his usual work corner. Suddenly the old man stopped writing, there was no more clicking of the keyboard or rustling of the pen on paper, silence enveloped the whole room. Perhaps, the old man suddenly thought of something interesting, immediately, his hands reached for the keyboard and typed very quickly, on the screen, we see the interface of a website. A moment later, a music video appeared on the screen, the old man eagerly watched the screen, with a little tear in the corner of his eyes, his mouth smiling slightly, his lips occasionally moving to the melody of the song:</p><blockquote><em>Though time flows endlessly away</em><br><em>My life will forever miss those days</em> <br><em>The sun has risen early, clouds return late</em><br><em>Still want to keep so much love in my heart</em></blockquote><p>Perhaps this is the first love song that the old man liked. He recalls old memories. In 7th grade (secondary school in Vietnam), he used to secretly take his parents’ records to listen to when they were away. At that time, Dan Truong was a famous singer, and this song was one of the old man’s favorite songs of this singer. When doing homework, he often hummed the melody of the song. At school, he fell in love with many classmates, at that time it was just called liking, he never said it out loud. Later, when grown up, whenever he felt sad about emotional matters, he often listened to this song again. He liked to gnaw at old sorrows. It’s not lovesick, he thoroughly enjoyed the slightly sad but lingering feeling. According to him, it was a good experience for anyone who enjoys doing work that requires emotion and a bit of artistry. Humming along with the music was also a way to confide in himself.</p><p>At the age of 80, the song is still a bridge for him to Vietnam, to his youth and beautiful memories that he left behind when leaving his homeland. The music is still playing in the room, outside the window, snow is falling white covering the night sky. Somewhere in this Aspen town, is there anyone else who is also a stranger missing their homeland? Looking out the window, he missed Saigon rainy days. He was riding a motorcycle on the road, heavy rain accompanied by strong wind slapped raindrops hard into his eyes, had to try hard to lift his eyelids to see the road. From behind, encircling his waist were the hands of a little girl, short shoulder-length hair. They were singing, laughing, enduring but also enjoying a cold rainy afternoon in Saigon. He missed it so much, the smell of the rain, the smell of Saigon after the rain, it was more alluring than any French perfume one could find in this foreign land.</p><p>Missing the smell of Saigon, a small tear rolled down from his eyes to his cheeks and fell lightly onto the notebook he was writing. Far away out there, the snow kept falling incessantly…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=622e473e5c2f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tự Truyện Hư Cấu]]></title>
            <link>https://medium.com/@huy.phung.sw/t%E1%BB%B1-truy%E1%BB%87n-h%C6%B0-c%E1%BA%A5u-ef84203a2857?source=rss-32b96c6f85c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef84203a2857</guid>
            <category><![CDATA[fiction-writing]]></category>
            <category><![CDATA[saigon]]></category>
            <category><![CDATA[fiction]]></category>
            <category><![CDATA[literature]]></category>
            <dc:creator><![CDATA[Harry Phung]]></dc:creator>
            <pubDate>Sat, 03 Jun 2023 17:50:05 GMT</pubDate>
            <atom:updated>2023-06-03T17:51:57.082Z</atom:updated>
            <content:encoded><![CDATA[<h3>Tự Truyện Hư Cấu 1</h3><p>Hoàng hôn, mặt trời từ từ lặng xuống sau rặng núi bao bọc thị trấn Aspen, bang Colorado, Hoa Kỳ. Tuyết phủ trắng mặt đường và dọc các triền lúi, lấm tấm đâu đó là những đốm đen hay nâu của những tán thông. Nhìn xa ra về phía ngọn đồi đối diện là một biệt thự nhỏ. Mùa này, tuyết phủ đầy trên những ngọn cây trong vườn.</p><p>Phía trên tầng hai, một vài ô cửa sổ hắt ánh đèn vàng ấm cúng. Ở một ô cửa sổ ngay góc phòng, bóng một người đàn ông đang ngồi cặm cụi, có lẽ là đang làm việc, cũng có thể là đang đọc sách. Một vị trí lý tưởng để đọc sách và ngắm nhìn thành phố mùa Giáng Sinh.</p><p>Căn phòng tầng hai này không quá rộng. Ở góc phòng, là hai kệ sách lớn, mỗi kệ cao gần hai mét và rộng khoảng một mét hai. Kệ sát góc phòng nhất chất đầy những cuốn sách về khoa học máy tính (Computer Science). Lướt ngang qua các tựa sách, ta nhận ra vài quyển về Ngôn Ngữ Lập Trình (Programming Language), một về quyển khác là về chủ đề Cơ Sở Dữ Liệu (Database), và nhiều sách khác nữa. Kệ còn lại, gần hơn về phía bàn làm việc gần cửa sổ là một kệ sách thú vị hơn gấp bội. Loáng thoáng đâu đó ta nhận ra quyển Bố Già (The GodFather) bản dịch của Ngọc Thứ Lan. Cạnh đấy là Life Of Pi bản dịch của Trịnh Lữ. Hai cuốn sách đã khá cũ, có lẽ được mang từ Việt Nam sang. Lấp ló trong kệ là Sherlock Holmes của Conan Doyle, vài cuốn truyện khác của Agatha Christie, Edogawa Ranpo và nhiều truyện trinh thám khác.</p><p>Đối diện hai tủ sách ở phía ngược lại là một tủ trưng bày khá to. Ngăn dưới cùng là vài bộ Lego đã được xếp ngay ngắn. Ngăn ở giữa và trên cùng là xếp lớp hàng loạt bàn phím cơ (Mechanical Keyboards) đủ loại. Có loại vỏ nhựa, có loại vỏ nhôm nhưng cũng có loại thân trong suốt, hay bằng gỗ.</p><p>Trên nóc tủ trưng bày, là một khung hình lớn. Sát rìa phải hình là hai chú chó Golden một nâu một trắng, đứng bằng hai chân, hơi chồm sang phía bên trái một chút để nhìn vào một cặp tình nhân đang ngồi ở giữa khung hình. Ở giữa là một thanh niên trong độ tuổi từ 25 đến 30. Tuy là người Châu Á nhưng anh có nước da sáng và vẻ ngoài ưa nhìn. Tóc trải ngược lên, xếp gọn gàng, kiểu tóc khá phổ biến của thập niên 80. Mắt anh nhìn thẳng xuống tay, tay anh đang nắm tay cô gái bên cạnh, miệng anh cười rạng rỡ. Cô gái ngồi kế anh có nước da sáng không kém, nổi bật là kiểu tóc ngắn ngang vai trông rất quý phái. Cô gái nổi bật giữa bức hình trong một bộ váy trắng, mắt nhắm nhưng như biết cười, miệng cô nhoẻn một nụ cười hạnh phúc. Khung hình tuy hơi ngã màu của thời gian, nhưng được giữ gìn rất sạch sẽ, trái với lớp bụi mỏng xung quanh. Cạnh khung hình là một hộp gỗ nhỏ, hơi bám bụi, mở hộp ra, ta có thể thấy nhiều loại bút máy khác nhau, nào là Kaweco, LAMY hay TWSBI. Sát hộp gỗ là nhiều lọ mực bút máy đủ màu, nhưng mực xanh là nhiều hơn cả.</p><p>Giữa phòng, cạnh cửa sổ là bàn làm việc dài hai mét. Bàn rộng nhưng không nhiều đồ, các vật dụng như loa, bàn phím và chuột, toàn bộ đều không dây. Màn hình rộng, được treo trên một cái arm trong rất gọn ghẽ. Đối diện màn hình, người đàn ông đang miệt mài đánh máy, đôi tay run run nhưng vẫn rất thanh thoát trong từng cử động. Khuôn mặt gầy, có nhiều nét nhăn trên mi mắt và hai bên gò má. Tuy đã xuất hiện những vết đồi mồi, nhưng nước da trắng vẫn tôn lên nét mặt sáng sủa của ông. Nếu nhìn kỹ, ta dễ dàng nhận ra đây là người thanh niên trong hình, nói chính xác hơn thì chàng thanh niên trong hình chính là thời niên thiếu của cụ già trong phòng. Đánh máy nhanh là thế, thỉnh thoảng cụ vẫn mở sổ tay trên bàn ghi ghi chép chép gì đó. Đã hơn mười giờ tối, nhưng cụ vẫn say xưa ngồi ở góc làm việc của mình. Chợt cụ dừng bút, không còn tiếng gõ phím lách cách hay tiếng ngòi bút sột soạt trên giấy, sự im lặng bao trùm cả căn phòng. Có lẽ, cụ chợt nghĩ ra thứ gì đó thú vị, ngay lặp tức, tay cụ với lấy bàn phím gõ gõ rất nhanh, trên màn hình, ta thấy hiện ra là giao diện của một trang web. Một lúc sau, trên màn hình hiện ra một video clip ca nhạc, cụ say mê nhìn vào màn hình, có một chút nước mắt ứ ở vành mi, miệng cười nhẹ, môi thỉnh thoảng mấp máy theo giai điệu bài hát:</p><blockquote>Mặc cho thời gian trôi xa ngút ngàn<br>Đời anh còn mãi mãi nhớ thương bao ngày<br>Nắng đã lên sớm mai mây về cuối trời<br>Vẫn muốn giữ lấy bao nhiêu yêu thương trong trái tim.</blockquote><p>Có lẽ đây là một bản nhạc tình yêu đầu tiên mà cụ thích. Cụ nhớ lại những ký ức cũ. Năm lớp 7 (cấp trung học cơ sở ở Việt Nam), cụ vẫn thường lén lấy đĩa nhạc của bố mẹ để nghe khi họ vắng nhà. Thời ấy, Đan Trường là một ca sĩ nổi tiếng, và bài này là một trong những bài hát mà cụ thích nhất của anh ca sĩ này. Những khi ngồi làm bài tập về nhà, cụ thường xuyên nghêu ngao giai điệu của bài hát. Thời đi học, cụ say đắm không ít cô bạn học cùng lớp, thời ấy chỉ gọi là thích, cụ nào có nói ra. Sau này, khi đã lớn, mỗi khi buồn về chuyện tình cảm, cụ vẫn thường nghe lại bài nhạc này. Cụ thích gặm nhấm những nỗi buồn cũ. Không phải là lụy tình, cụ hoàn toàn thích thú với cảm giác hơi buồn nhưng cũng hơi lưu luyến đó. Theo cụ, đó là một trải nghiệm tốt cho ai thích làm những công việc đòi hỏi cảm xúc và một ít chất nghệ sĩ trong đó. Ngân nga theo nhạc, cũng là một cách để tâm sự với chính bản thân mình.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*OCuvAgauqI4-c2plE3DXyQ.jpeg" /></figure><p>Ở tuổi 80, bản nhạc còn là cầu nối của cụ với Việt Nam, với tuổi trẻ và những kỷ niệm đẹp mà cụ đã bỏ lại khi rời quê hương. Tiếng nhạc vẫn vang lên trong phòng, bên ngoài cửa sổ, tuyết rơi trắng che lấp cả bầu trời đêm. Đâu đó trong thị trấn Aspen này, liệu có ai cũng là người xa xứ đang nhớ về quê hương mình? Nhìn ra cửa sổ, cụ nhớ về Sài Gòn những ngày mưa. Cụ đang phóng con xe trên đường, mưa lớn kèm gió mạnh quật từng hạt mưa phất mạnh vào mắt cụ, phải cố gắng lắm mới nhấc nỗi mi mắt để nhìn đường. Từ phía sau, vòng lên eo là đôi tay của một cô gái nhỏ, tóc ngắn ngang vai. Họ vừa hát, vừa cười, vừa chịu đựng nhưng cũng vừa tận hưởng một chiều mưa lạnh của Sài Gòn. Cụ nhớ lắm, cái mùi mưa ấy, cái mùi của Sài Gòn sau cơn mưa, nó quyến rũ hơn bất kỳ lọ nước hoa Pháp nào ta có thể tìm được ở nơi đất khách này.</p><p>Nhớ về mùi Sài Gòn, một giọt lệ nhỏ lăn trên dài từ mi mắt ông xuống gò má và rơi nhẹ xuống trang sổ cụ đang viết. Ở phía xa xa ngoài kia, tuyết vẫn liên tục rơi không ngừng…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef84203a2857" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[uniq and pluck]]></title>
            <link>https://medium.com/@huy.phung.sw/uniq-and-pluck-436cfe840584?source=rss-32b96c6f85c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/436cfe840584</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Harry Phung]]></dc:creator>
            <pubDate>Wed, 19 Sep 2018 18:44:20 GMT</pubDate>
            <atom:updated>2019-12-23T11:01:29.075Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Last week, when I try to review some codes by visiting many pull requests. I also took a little look at some classes that were written in <strong>Rubocop</strong>. There is a class name <strong>UniqBeforePluck</strong>. I did not know anything about it before, then I did a little research andI found out something that is very useful for my lack of knowledge.</blockquote><h3>Situation in theory</h3><p>ActiveRecord in rails they use <em>lazy_load</em> (<em>if you did not know how lazy load work:</em> <a href="https://rubyinrails.com/2014/01/08/what-is-lazy-loading-in-rails/">check here</a>). So if we have a model named Engineer and we want to do a query like this:</p><pre>Engineer.some_scope.pluck(:company_id).uniq</pre><p>If there are a lot of records for Engineer.some_scope. We can imagine that Engineer.some_scope return a large number of records and when the query is evaluated by .pluck the speed is slower than we use such thing below:</p><pre>Engineer.some_scope.uniq.pluck(:company_id)</pre><p>In this case, .uniq return the relation that created fewer records before it runs the next query .pluck. Create the code with .uniq before .pluck do not lead to any bug because <strong>ActiveRecord</strong> only evaluate the value after they are reaching to .pluck.</p><h3>Reality</h3><p><strong>However</strong>, it is in theory. In reality, if the number of a duplicate of company_id values in the query is not too much, they are run at the same speed and sometimes it is faster to use .pluck before .uniq. But if the number of duplicate values are too many. We still refer to use as the case above.</p><h3>Be careful</h3><p>We should only use .uniq before .pluck Only if we use it for Model. We need to be aware that this thing only applies for <strong>Active Relations,</strong> not for C<strong>ollection Proxy</strong>. For example, we have another model call Field:</p><pre>class Field &lt; ActiveRecord::Base<br>  has_many :engineers<br>end</pre><pre>field = Field.first<br>field.engineers.uniq.pluck(:company_id)</pre><p>In this case, use .uniq before .pluck lead to an error like this:</p><pre>NoMethodError: undefined method `pluck&#39; for #&lt;Array:0x007f9e708b3b28&gt;</pre><p>Why? Because `field.engineers` return a C<strong>ollection Proxy.</strong> After that .uniq change it to an array of records. No method .pluck for an array of records.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=436cfe840584" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Convert string with ROT13]]></title>
            <link>https://medium.com/@huy.phung.sw/solved-rot13-f83c6042d20c?source=rss-32b96c6f85c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/f83c6042d20c</guid>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[string]]></category>
            <dc:creator><![CDATA[Harry Phung]]></dc:creator>
            <pubDate>Wed, 12 Sep 2018 17:25:19 GMT</pubDate>
            <atom:updated>2019-12-23T10:54:26.716Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>I found this when doing some competitive coding exercises online. It looks funny, so I want to share.</blockquote><h3>Problem</h3><p>I have an array that contains many messages:</p><pre>[&#39;I love chocolate&#39;, &#39;GMK keycaps are my life&#39;, &#39;Cheers for Peers&#39;]</pre><p>I want to write a Ruby method that convert the messages array withROT13 :</p><pre>[&#39;V ybir pubpbyngr&#39;, &#39;TZX xrlpncf ner zl yvsr&#39;, &#39;Purref sbe Crref&#39;]</pre><p>What is ROT13? You can check it on <a href="https://en.wikipedia.org/wiki/ROT13">Wikipedia</a>.</p><h3>Using .map</h3><p>My very first solution that came to my mind after I read the problem. Because of the letter from A..M and a..m are map to N..Z and n..z, then I need to create a simple method that helps us to convert each letter first:</p><pre>def decode_char(char)<br>  case char<br>    when &#39;a&#39;..&#39;m&#39;, &#39;A&#39;..&#39;M&#39;<br>      char.ord + 13<br>    when &#39;n&#39;..&#39;z&#39;, &#39;N&#39;..&#39;Z&#39;<br>      char.ord - 13<br>    else<br>      char.ord<br>  end.chr<br>end</pre><p>After that, I will using .map to return each word in the message with the method:</p><pre>def decode_word(word)<br>  new_word = word.chars.map do |char|<br>    decode_char(char)<br>  end.join<br>end</pre><p>Finally, we can return the messages that decoded using ROT13</p><pre>def rot13(messages)<br>  messages.map do |message|<br>    decode_word(message)<br>  end<br>end</pre><h3>The short way</h3><p>Ruby also provide us a built-in name tr . This built-in is nothing new; it existed in Unix command a long time ago. We can read the uses of tr <a href="https://apidock.com/ruby/String/tr">here</a>.</p><pre>def rot13(messages)<br>  messages.map do |message|<br>    message.tr(&#39;A-Za-z&#39;, &#39;N-ZA-Mn-za-m&#39;)<br>  end<br>end</pre><p>If you do not understand how this code work, I can explain it here:</p><p>The first set specified is [A-Za-z], which is a shorthand way of typing [ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz].</p><p>The second is[N-ZA-Mn-za-m], which turns into [NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm].</p><p>tr reads each character from stdin, and if it appears in the first set, it replaces it with the character in the same position in the second set.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f83c6042d20c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading Note Sep 6th 2018]]></title>
            <link>https://medium.com/@huy.phung.sw/reading-note-sep-6th-2018-8ea1e476ec60?source=rss-32b96c6f85c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ea1e476ec60</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Harry Phung]]></dc:creator>
            <pubDate>Thu, 06 Sep 2018 02:46:47 GMT</pubDate>
            <atom:updated>2018-09-06T02:46:47.645Z</atom:updated>
            <content:encoded><![CDATA[<h3>Modules in Ruby</h3><p>Many object-oriented languages provide a way to define a named group of methods that are independent of class and can be mixed in to any object. In Ruby, these mixed-ins are called <strong><em>modules</em></strong><em>.</em></p><p>Once you start putting code into modules and adding modules to objects, you expand the set of messages to which an object can respond and enter a new realm of design complexity. An object that directly implements few methods might still have a very large response set. The total set of messages to which an object can respond includes:</p><p>• Those it implements<br> • Those implemented in all objects above it in the hierarchy<br> • Those implemented in any module that has been added to it<br> • Those implemented in all modules added to any object above it in the hierarchy</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ea1e476ec60" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Something I did with .tap]]></title>
            <link>https://medium.com/@huy.phung.sw/something-i-did-with-tap-a8e0f83ef0a2?source=rss-32b96c6f85c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8e0f83ef0a2</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Harry Phung]]></dc:creator>
            <pubDate>Wed, 05 Sep 2018 06:38:28 GMT</pubDate>
            <atom:updated>2019-12-23T10:48:40.694Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>I have worked with Ruby on Rails for more than 15 months, but I rarely use the .tap method. In the last week, I found out that it is useful for some cases. However, it can cause some trouble if you do not understand how the codes work actually.</blockquote><p>.tap code in Ruby show below:</p><pre><strong>class Object</strong><br>  <strong>def tap</strong><br>    <strong>yield</strong> self<br>    self<br>  <strong>end<br>end</strong></pre><p>We used .tap when we called some methods on an object, but we do not want to get the final value but the object instead. And in the last week, I encountered two cases that I have to use .tap</p><h3><strong>First case</strong></h3><p>I have the codes below:</p><pre><strong><br>def</strong> update_info(info)<br>  user<strong> </strong>= <strong>User</strong>.find_by(name: info[:name])<br>  user.address = info[:address]<br>  user.phone_number = info[:phone_number]<br>  user.<strong>save!</strong><br>  user<br><strong>end</strong></pre><p>The purposes of this method are:</p><ul><li>Update the user object and save it to the database</li><li>Return the user object after save</li></ul><p>But I do not want to return the object like that. The user line at the end of that method seems extraneous. So, we can clean it up with the new implementation below:</p><pre><strong>def</strong> update_info(info)<br>  <strong>User</strong>.find_by(name: info[:name]).tap do |u|<br>    u.address = info[:address]<br>    u.phone_number = info[:phone_number]<br>    u.<strong>save!</strong><br>  end<br><strong>end</strong></pre><h3>Second case</h3><p>In the second case, I made a mistake when using .tap with assignment. The original code of the second case is:</p><pre><strong>def</strong> my_elements<br>  elements = []<br>  elements += <strong>FIRST_ELEMENTS</strong> if first_condition<br>  elements += <strong>SECOND_ELEMENTS</strong> if second_condition<br>  elements<br><strong>end</strong></pre><p>In the first try, I made a mistake by refactor the code like this:</p><pre><strong>def</strong> my_elements<br>  []<strong>.tap</strong> do |elements|<br>    elements += <strong>FIRST_ELEMENTS</strong> if first_condition<br>    elements += <strong>SECOND_ELEMENTS</strong> if second_condition<br>  <strong>end</strong><br><strong>end</strong></pre><p>In this case the elements is pointing to the array object but if we use assignment here, it do not mutate the original array, it will only change the pointer of elements to the new array it is assign. But in the end, .tap will return the original array and in this case, it always return an <strong>empty array</strong>.</p><p>So I try another thing to solve this problem. Instead of using assignment like the origin codes. I used .concat . Concat will mutate the current array by appending the elements of other array to itself. Finally, .tap will return the correct result that we want.</p><h3>Conclusion</h3><p>By go through the two cases above, I hope I shared some good experiences about using .tap. This method is excellent for making your codes look clean. However, we need to notice the mutation. I suggest write the unit tests first before we refactor any codes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8e0f83ef0a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Template Method in Ruby]]></title>
            <link>https://medium.com/@huy.phung.sw/template-method-in-ruby-24ab11dad400?source=rss-32b96c6f85c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/24ab11dad400</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[mechanical-keyboards]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Harry Phung]]></dc:creator>
            <pubDate>Sat, 25 Aug 2018 17:32:09 GMT</pubDate>
            <atom:updated>2019-12-23T10:29:42.731Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Nowadays, as the field of software development grows. Besides of fast implementation of the products, the engineers also need to write the code that looks clean and easier to add new codes in the future.</p><p>One of the most straightforward but useful design patterns is the Template Method. The Template Method allows you to define the “skeleton” of the codes while leaving concrete implementation up to inheriting classes.</p><h3>Implementation</h3><p>Assume that we are a company that selling mechanical keyboards. Mechanical keyboards are different from the regular keyboards because of their switches. Imagine that we want to write the class to generate the information of the switch like the codes below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76e5a2d845440e1ee84031887646e323/href">https://medium.com/media/76e5a2d845440e1ee84031887646e323/href</a></iframe><p>As we can see, the class is messy with the <strong>feeling_rating</strong> and <strong>recommended_for</strong> methods. With the Template method, we can resolve the problem by split the concrete code into TactileSwitch and LinearSwitch. Make the first Switch class become the abstract one. The code and instruction above only show the concept of implementing a Hash Tables class by yourself. In reality, a good Hash Tables uses Dynamic Buckets to use memory effectively.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8bd00c5b2b68abffc81b9644ede4d5a2/href">https://medium.com/media/8bd00c5b2b68abffc81b9644ede4d5a2/href</a></iframe><p>This type of coding called the Template Method. We move the different parts out of the super-class, make the super-class abstract. It help us to separate the code change into the code that doesn’t. So it makes our concern look clean and safer to change in the future.</p><h3>Trade-off</h3><p>The Template Method can help us to keep our code SOLID and DRY. It is beneficial if the part of the context is different from the others, but if multiple parts of our codes are different. The number of concrete subclasses can quickly get out of hand. So please think twice if you apply this pattern.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24ab11dad400" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reading Note Aug 18th 2018]]></title>
            <link>https://medium.com/@huy.phung.sw/reading-note-aug-18th-2018-ce7ba98a1849?source=rss-32b96c6f85c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce7ba98a1849</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Harry Phung]]></dc:creator>
            <pubDate>Sat, 18 Aug 2018 03:00:38 GMT</pubDate>
            <atom:updated>2018-08-18T03:02:25.598Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Static Typing</strong></h3><p>Static and dynamic typing both make promises and each has costs and benefits. Static typing aficionados cite the following qualities:</p><ul><li>The compiler unearths type errors at compile time.</li><li>Visible type information serves as documentation.</li><li>Compiled code is optimized to run quickly.</li></ul><p>These qualities represent strengths in a programming language only if you accept this set of corresponding assumptions:</p><ul><li>Runtime type errors will occur unless the compiler performs type checks.</li><li>Programmers will not otherwise understand the code; they cannot infer an object’s type from its context.</li><li>The application will run too slowly without these optimizations.</li></ul><h3><strong>Dynamic Typing</strong></h3><p>Dynamic typing proponents list these qualities:</p><p>• Code is interpreted and can be dynamically loaded; there is no compile/make cycle. • Source code does not include explicit type information.<br> • Metaprogramming is easier.</p><p>These qualities are strengths if you accept this set of assumptions:</p><ul><li>Overall application development is faster without a compile/make cycle.</li><li>Programmers find the code easier to understand when it does not contain type declarations; they can infer an object’s type from its context.</li><li>Metaprogramming is a desirable language feature.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce7ba98a1849" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>