Ruby

Versions

CircleCI: https://hub.docker.com/r/circleci/ruby/tags?page=1&ordering=last_updated

Debugging

https://github.com/ruby/debug

Gemfile:

gem "debug", ">= 1.0.0"

Application or test code:

debugger

Disabling Docs

~/.gemrc

gem: --no-document

Creating a Standalone Ruby Project

$ mkdir my-project
$ cd my-project
$ git init .
$ npx gitignore ruby
$ bundle init

To create a gem see Ruby Gems

Dates and Times

  • Date: date only
  • Time: dates and times, and DST, but doesn’t handle historic calendar reforms. Active Support adds time zone
  • DateTime: handles historic calendar reforms, but not DST. Deprecated

https://stackoverflow.com/questions/1261329/difference-between-datetime-and-time-in-ruby

Time.now
Time.parse('...')

Info on time zones on Rails

Aliasing

  • stdlib alias new_name old_name (has to be statically defined; not symbols, just the method names)
  • Rails alias_attribute :new_name, :old_name (allows dynamic)

Delegation

  • stdlib Delegator: requests are automatically forwarded from a master class to delegates (also SimpleDelegator)
  • stdlib Forwardable: allow classes to delegate named method calls to other objects
  • Active Support delegate :method, to: :other_object

Shortcuts

  • .map(&:method) - & passes a Proc as a block. Since it is given a symbol instead of a block, it calls #to_proc on the symbol first. Symbol#to_proc creates a Proc that corresponds to the method with the name of the symbol.

Heredocs

  • << - terminator must not be indented
  • <<- - can indent terminator, other indentation preserved
  • <<~ - squiggly heredoc: The indentation of the least-indented line will be removed from each line of the content.

Regexes

/cat/ =~ ​"dog and cat"​ ​# => 8
​"dog and cat"​ =~ /cat/ ​# => 8​
new_str = str.sub(/Cat/, ​"Gerbil"​) # one
new_str2 = str.gsub(/a/, ​"*"​) # global

match_data = /cat/.match("dog and cat")

Ruby Version Features

  • https://www.rubyguides.com/ruby-version-changes/
  • https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/

  • 2.0
    • Keyword arguments (instead of a hash)
    • UTF-8 default
  • 2.1
    • Required keyword arguments
  • 2.3
    • Frozen strings pragma
    • Safe navigation operator: &.
    • Squiggly heredoc: <<~
    • Hash#dig
  • 2.4
    • Fixnum + Bignum merge into Integer
  • 2.5
    • #yield_self
  • 2.6
    • Endless ranges
    • #then an alias for #yield_self
    • Enumerable#filter
  • 2.7
    • Pattern matching (experimental)
    • Numbered block parameters
  • 3.0
    • Reached goal of 3x performance of 2.0 on some benchmarks
    • Ractor concurrency model (experimental)
    • Fiber#scheduler lightweight concurrency
    • RBS
    • Rightward assignment: =>
    • “Endless” method definition (that is, one-line method definition: def method = …)
    • Keyword arguments and hash parameters no longer interchangeable
  • 3.1
    • Hash value omission
    • One-line pattern matching

Modules

# safest to nest modules every time you reference them

# best
module A
  class B
  end
end

# riskier
class A::B
end
  • Zeitwork seems to mostly do the right thing with the :: approach
  • But if a user manually requires the file it might not be right