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

Ruby instance_eval & block

```rb class Test def initialize(&block) @name = "Hello World" instance_eval(&block) if block_given? end def name @n...

Wayne

不要在 rake task 裡定義 method

因為 rake task 的 scope 是 global 的,如果在裡面定義 method 等於是為 Object class 定義 private method,污染全部的 code 可行的做法之一是把要跑的 task 寫成 service 執行 另外推薦的做法是...

Wayne

2018 Ruby Kaigi 網路上看到的懶人整理

* Matz considering alias yied_self with then * Rib, an alternative for irb * Stripe team building Ruby type checker https://blog....

Wayne

http.rb replace net::http

作者認為 Net::Http 雖然是 standard lib 之一,但 API 設計很不好,底層實作也有很多覺得不滿的地方,包含 Net::Http 的 Error 封裝方式,讓人不知道怎麼 rescue 有機會可以試用看看 https://github.com/h...

Wayne