A princípio, pode parecer estranho que um objeto não possa ser alterado após a sua criação, mas isso é algo que acontece com alguns tipos de objetos no Ruby, como por exemplo, Symbol e Integer.
Nesse post, abordo o conceito de imutabilidade no Ruby, discutindo como criar objetos imutáveis e explorando os benefícios dessa abordagem.
Você pode ver mais aqui 👇🏻
https://aristotelescoutinho.com.br/entendendo-objetos-imutaveis-no-ruby/
I've been copying a variation of this snippet into most Ruby projects:
class BigDecimal
def inspect
"BigDecimal(\"#{to_s(?F)}\")"
end
end
It overrides the default appearance of a BigDecimal in an irb/pry/rails console. Why?
1. Easier to visually scan than exponential notation
2. Can be copy-pasted to get another bigdecimal (otherwise using exp notation yields a Float instance)
When you use the block syntax with Hash.new to define a default value, you lock yourself to only using the square bracket syntax for value lookup. The fetch method ignores the block, default, and default_proc.
This bit me hard this week, and my pair, @gd, got to watch me flounder on why fetch wasn’t getting the default value.
By the way, the Hash.new with a block is great for memorization of calculated or network lookup data.
#ruby #rubytips #rubygotchas #thisweekilearned #twil
Nice. TL;DR: to measure elapsed time, don't use http://Time.now, use Process.clock_gettime(Process::CLOCK_MONOTONIC)
#RubyTips #Ruby
RT @piotr_murach@twitter.com
When measuring elapsed time in Ruby refrain from using http://Time.now call. Why? It is prone to "time drifting" and "errors in increase frequency" which may cause a negative time interval!😱
If you want to know more, read this 👉
https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/ by @jodosha@twitter.com
🐦🔗: https://twitter.com/piotr_murach/status/1229117605383897088