<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kai Sasaki</title>
    <description>The latest articles on DEV Community by Kai Sasaki (@lewuathe).</description>
    <link>https://dev.to/lewuathe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F37372%2F1effba93-fc22-4a84-93dd-340db514e78d.jpg</url>
      <title>DEV Community: Kai Sasaki</title>
      <link>https://dev.to/lewuathe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lewuathe"/>
    <language>en</language>
    <item>
      <title>How to achieve ARM cross-compilation on macOS</title>
      <dc:creator>Kai Sasaki</dc:creator>
      <pubDate>Wed, 24 Jun 2020 06:58:54 +0000</pubDate>
      <link>https://dev.to/lewuathe/how-to-achieve-arm-cross-compilation-on-macos-3b08</link>
      <guid>https://dev.to/lewuathe/how-to-achieve-arm-cross-compilation-on-macos-3b08</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article originally comes from &lt;a href="https://www.lewuathe.com/how-to-make-arm-cross-compilation-on-macos.html"&gt;lewuathe.com&lt;/a&gt;. Please visit there too if you find this interesting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cross-compilation is occasionally troublesome. It often requires us to install additional toolchain to get it done.&lt;/p&gt;

&lt;p&gt;Now, I found I needed to compile my source code for the Arm architecture machine for &lt;a href="https://github.com/Lewuathe/mbed-sample"&gt;Mbed application&lt;/a&gt;. I do not have any idea how to install the toolchain for Arm compilation. Thus this is the post to describe the fastest way to prepare the toolchain for the cross-compilation for Arm.&lt;/p&gt;

&lt;h1&gt;
  
  
  Xcode Compiler
&lt;/h1&gt;

&lt;p&gt;Please ensure to install Xcode first. Xcode contains the first compiler sets, such as clang.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ xcode-select --install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Arm Toolchains from Homebrew
&lt;/h1&gt;

&lt;p&gt;You can install Arm GCC toolchains by using Homebrew. There are two options.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/osx-cross/homebrew-arm"&gt;osx-cross/homebrew-arm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/ARMmbed/homebrew-formulae"&gt;ARMmbed/homebrew-formulae&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is no difference between these two distributions. They use precisely the same package internally. You can use whichever you want.&lt;br&gt;
For my Mbed application, I choose the second one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew tap ArmMbed/homebrew-formulae
$ brew install arm-none-eabi-gcc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should be able to find the compiler eventually. The following tools are installed in the local machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ arm-none-eabi-gcc
$ arm-none-eabi-g++
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It looks like they are GCC for Arm architecture. I believe it is the easiest way to prepare the Arm build toolchains in macOS platform.&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

</description>
      <category>macos</category>
      <category>arm</category>
      <category>compiler</category>
    </item>
    <item>
      <title>A Reader to Writer</title>
      <dc:creator>Kai Sasaki</dc:creator>
      <pubDate>Mon, 22 Jun 2020 00:43:21 +0000</pubDate>
      <link>https://dev.to/lewuathe/a-reader-to-writer-2kek</link>
      <guid>https://dev.to/lewuathe/a-reader-to-writer-2kek</guid>
      <description>&lt;p&gt;For a long time, I was an enthusiastic reader of &lt;a href="https://dev.to/"&gt;dev.to&lt;/a&gt;. The platform has provided me a lot of inspiring articles to accelerate my skill and knowledge around various kinds of technologies as a developer. Now, I decided to contribute my own experience by writing posts here as others did. I hope my writing will help someone others in the field develop their own skills as I did in the dev.to in the past several years.&lt;/p&gt;

&lt;p&gt;Thanks and hello!&lt;/p&gt;

</description>
      <category>hello</category>
      <category>greeting</category>
    </item>
    <item>
      <title>Why we should avoid default_scope in Rails</title>
      <dc:creator>Kai Sasaki</dc:creator>
      <pubDate>Mon, 22 Jun 2020 00:11:53 +0000</pubDate>
      <link>https://dev.to/lewuathe/why-we-should-avoid-defaultscope-in-rails-3ofo</link>
      <guid>https://dev.to/lewuathe/why-we-should-avoid-defaultscope-in-rails-3ofo</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article originally comes from &lt;a href="https://www.lewuathe.com/why-we-should-avoid-default_scope-in-rails.html"&gt;lewuathe.com&lt;/a&gt;. Please visit &lt;a href="https://www.lewuathe.com/"&gt;here&lt;/a&gt; as well if you enjoy it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;ActiveRecord in Rails provides a way called &lt;a href="https://guides.rubyonrails.org/active_record_querying.html#scopes"&gt;&lt;code&gt;scope&lt;/code&gt;&lt;/a&gt; to keep the readability along with encapsulating the detail of the business logic in the model class. It enables us to add a more intuitive interface to the model so that we can quickly call the scoped method without caring about the complicated underlying implementation. This also contributes to achieving the well-known good practice in the MVC model, &lt;a href="https://riptutorial.com/ruby-on-rails/example/9609/fat-model--skinny-controller"&gt;&lt;strong&gt;"Fat Model, Skinny Controller"&lt;/strong&gt;&lt;/a&gt;. It shows us the clear guidance saying, "We should not write non-response related logic in the controller". If you are writing a complicated logic that is not directly related to the HTTP response construction response, that should go to the model, not controller. &lt;code&gt;scope&lt;/code&gt; methods are helpful to materialize this goal.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;default_scope&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;As part of the scope feature, ActiveModel has a &lt;code&gt;default_scope&lt;/code&gt; which defines the scope method applied to all queries on the model. Let's say we have a &lt;code&gt;User&lt;/code&gt; model as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;User.all&lt;/code&gt; returns all users as it states. But what if you want to get the users excluding all hidden users. The following code will return the results as you expected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;hidden: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But &lt;code&gt;default_scope&lt;/code&gt; will provide a more convincing manner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;default_scope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;hidden: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;default_scope&lt;/code&gt; is always applied to the model query. In other words, you do not need to specify the query explicitly anymore.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt; &lt;span class="c1"&gt;# It will return the visible users, excluding hidden ones.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That is good. You do not need to specify the same &lt;code&gt;where&lt;/code&gt; conditions many times. &lt;code&gt;default_scope&lt;/code&gt; automatically creates the basis of all queries.&lt;/p&gt;

&lt;p&gt;Practically, &lt;code&gt;default_scope&lt;/code&gt; is often not recommended in Rails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implicit Behavior Change
&lt;/h2&gt;

&lt;p&gt;Based on my experience, the biggest problem of the &lt;code&gt;default_scope&lt;/code&gt; is applied implicitly. If the writer of the &lt;code&gt;default_scope&lt;/code&gt; is different from the model user, the behavior must look weird. Model users will see a query they do not write unexpectedly. Implicit behavior change is generally anti-pattern. (In Scala, &lt;a href="https://docs.scala-lang.org/tour/implicit-conversions.html"&gt;even the compiler shows the warning for the &lt;code&gt;implicit&lt;/code&gt; type conversion.&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In my case, I have developed one API using the model class, which is derived from the original web application. Since the data source is shared with them, it is useful to share the model class too. But it brings unexpected pitfall caused by &lt;code&gt;default_scope&lt;/code&gt;. At some time, another developer introduced the following &lt;code&gt;default_scope&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OriginalClass&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;default_scope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;all_columns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;An application I have developed is using the class. What I want here are only &lt;code&gt;c1&lt;/code&gt;, &lt;code&gt;c2&lt;/code&gt;, and &lt;code&gt;c3&lt;/code&gt;. Returning all columns can cause the problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;OrignalClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"c1 = xxx"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"c1, c2, c3"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you imagine, introducing the &lt;code&gt;default_scope&lt;/code&gt; here makes it happen. Without any notice, all columns are returned because I do not know the change around the default behavior of the &lt;code&gt;OriginalClass&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Implicit behavior change is always requiring intensive care. All developers touching the codebase and related repository need to be careful of the transformation of the behavior. But we must not expect all members to do so. It's unrealistic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use &lt;code&gt;scope&lt;/code&gt;, not &lt;code&gt;default_scope&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Here is a simple answer. Use &lt;code&gt;scope&lt;/code&gt;, not &lt;code&gt;default_scope&lt;/code&gt;. What we want to do was completely achieved by &lt;code&gt;scope&lt;/code&gt;. There was no special reason to use &lt;code&gt;default_scope&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OriginalClass&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_all_columns&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;all_columns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;scope&lt;/code&gt; does not break any user codebase implicitly. If a user wants to make use of this new scope, call it explicitly. Of course, &lt;code&gt;default_scope&lt;/code&gt; can reduce the amount of code you need to write in terms of the number of characters. But the damage and maintenance cost will surpass the benefit obtained by the &lt;code&gt;default_scope&lt;/code&gt;. Simply obeying the following guidance will lead you to keep the Rails code clean and more maintainable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use scope, not default_scope&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://andycroll.com/ruby/dont-use-default-scope/"&gt;Don’t use default_scope. Ever.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/25087336/why-is-using-the-rails-default-scope-often-recommend-against"&gt;Why is using the rails default_scope often recommend against?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>tips</category>
    </item>
  </channel>
</rss>
