Fix timestamp precision problem in CI

用 Timecop freeze 時間時,會因為作業系統不同的關係導致 RSpec test 在 compare timestamp 時出現誤差值,導致在 local 能 pass 的 test case 在 CI 卻會 failed,以下提供看似正常但會出錯的 Test ...

Wayne

Readable feature test with RSpec

可以用更語意的 method 來寫 spec,with `given`, `when`, `and`, `then` keywords Example: ```rb feature 'Enrolment' do scenario 'Enrolling in a c...

Wayne

Feature toggle (a.k.a Feature Flags) in Ruby and gems to use

> Feature Toggles (often also refered to as Feature Flags) are a powerful technique, allowing teams to modify system behavior without cha...

Wayne

#super_method in Ruby

By calling `super_method`, you can send message to parent class. It could be used to test decorator or protocol. ```rb class ParentClass...

Wayne

Weird warning with Ruby 2.7

I kept having this `The called method '…' is defined here` warning from my rails console. Ex: ``` /Users/wayne/.rvm/gems/ruby-2.7.0/gems...

Wayne

Ruby 2.7 pattern matching official docs has released

早前一篇文章 [New features in Ruby 2.7](/posts/361-new-features-in-ruby-2-7) 提到了 Ruby 2.7 將要有 pattern matching,現在 [official docs](https://docs....

Wayne

寫 Ruby gem 的 best practice

寫 Ruby gem 的 best practice,滿受用的但太長了有空再來看,先記一下,please check the referecne ### Referecne - https://piotrmurach.com/articles/writing-a-rub...

Wayne

How to pass arguments to rake task

1. rake way ```rb task :add, [:num1, :num] do |t, args| puts args[:num1].to_i + args[:num].to_i end # rake add\[1,2\] ``` 2. ENV ``...

Wayne

Ruby under a microscope - Chapter 1 - Tokenization and Parsing

This is my reading note about the book `Ruby under a microscope` Chapter 1 - Tokenization and Parsing 第一章在講我們寫的 ruby code 是怎麼經過一系列的轉換最後...

Wayne

Ruby HTTP client options

- The built-in net/http client - Faraday - http.rb (HTTP The Gem) - rest-client - httparty - excon - Typhoeus - Curb For some pros and c...

Wayne

ruby lambda and proc

I found this article has a clear explanation of proc and lambda and provides good example of them. https://dev.to/keithrbennett/lambdas-...

Wayne

Ruby API serializer options

這週 Ruby Weekly 剛好兩個人在推自己的 serializer gem,大家都要自己做自己的工具,驗證了別人寫的 code 都是屎,只有自己寫的才是最好的真理,呵呵。 趁機整理一下: - https://github.com/rails-api/active_...

Wayne

Ruby fibrers

簡單來說可以把 Fiber 想成是 Thread 但他們的差別在於 Thread 的運作和停止是由 Operation System 控制,Fiber 則是由 Programmer 控制。丟系安捏。 ` Fiber.yield` 可以 stop Fiber 並把程序...

Wayne

New features in Ruby 2.7

最令我在意的就是 pattern matching ,但是是 experimental feature,可能還得再等等。 https://www.ruby-lang.org/en/news/2019/12/25/ruby-2-7-0-released/

Wayne

Ruby 2.7 懶人點點點語法

`...` 成為最新最潮的 arguments forwarding syntax ```rb def bar(*args, **kws, &block) block.call(args, kws) end ``` 可以寫成 ```rb def foo(...)...

Wayne

Unexpected behaviour for ActiveModel::Model validate and valid?

Assume we have an `FakeUser` class looks like below: ```rb class FakeUser include ActiveModel::Model include ActiveModel::Attribute...

Wayne

Redefine comparator in Python and Ruby

## Python In python, there are several magic functions for comparing: * `__eq__(self, other)` 定義了等號的行為` ==` 。 * `__ne__(self, other)` ...

Wayne

How Ruby REALLY uses Memory: On the Web and Beyond

好文,強烈建議閱讀全文 => https://www.schneems.com/2019/11/06/how-ruby-really-uses-memory-on-the-web-and-beyond/ ## TL;DR: - Tread 越多 memory usag...

Wayne

Ruby yield_self and then syntax

## TL;DR: 1. `yield_self` == `then` 2. `yield_self` returns the result of the block ## yield_self Introduced in Ruby v2.5 Difference...

Wayne

Ruby multiline string

## With indention version ```rb indented = <<-EOS Hello This is an example. I have to write this multiline string outside the we...

Wayne

Keyword argument in Ruby 3

https://blog.saeloun.com/2019/10/07/ruby-2-7-keyword-arguments-redesign.html

Wayne

Rails ActiveModel with type casting

## Basic usage Use `include ActiveModel::Model` and `include ActiveModel::Attributes` can provide type casting for active model. Exampl...

Wayne

在 Rails 裡擴充 gem 提供的 ActiveRecord model 的方法

需求:想要為 Gem 提供的 Model 增加一些新的 method 或是 association For example, doorkeeper 的 gem 有一個 `Doorkeeper::AccessToken` 的 model,我想要有個 `Doorkeeper:...

Wayne

Ruby 2.6 的 Range 可以 accept nil 當作 Float::INFINITY

### Before 2.6 (v < 2.5) ```rb end_of_range = nil (1..end_of_range).map { |i| do_something(i) } #=> ArgumentError (bad value for rang...

Wayne

Function Composition in Ruby

Ruby 2.6 introduced `<<` and `>>` methods, this article explains function composition and history of Ruby new methods throughoutly, wort...

Wayne