Skip to content

Ruby & Rails

alex [dot] kramer [at] g_m_a_i_l [dot] com edited this page Jan 9, 2023 · 13 revisions

Upgrade rbenv ruby version

brew update && brew upgrade ruby-build && rbenv install 3.1.2 && rbenv global 3.1.2

Private module methods

module M
  def self.pub
    puts "pub!"
	self.priv
  end
  
  def self.priv
    puts "priv!"
  end
  
  class << self
    private :priv
  end
end

Rails

ActiveRecord Queries

# Get all Things sorted by creation time
latest_things = Thing.where(some_column: "some value").sort_by(&:created_at)

Check and run migrations

rails db:migrate:status
bundle exec rails db:migrate

Rspec

Capybara (browser based test scenarios; ruby)
                  │
                  ▼
Selenium ruby API (ruby API that maps to Selenese,
the Selenium browser-automation scripting language)
                  │
                  ▼
Driver (e.g. Chromedriver; browser-specific bindings
for executing Selenese scripts against a particular browser)

Gems

HTTParty

Turn on debug logging:

response = HTTParty.post(
  "https://tw.yahoo.com",
  headers: headers,
  body: body.to_json,
  debug_output: $stdout
)

Wavefile

https://wavefilegem.com/

Generate descending saw tone

require "wavefile"
include WaveFile

SAMPLE_RATE = 44100
FORMAT = Format.new(:mono, :float, SAMPLE_RATE)

def square(lambda, vol, duration_ms)
  num_cycles = (duration_ms/1000.0) * (SAMPLE_RATE / lambda)
  ([vol] * (lambda/2) + [-vol] * (lambda/2)) * num_cycles
end

Writer.new("/tmp/descending.wav", Format.new(:mono, :pcm_16, SAMPLE_RATE)) do |writer|
  for lambda in (1..1000)
    writer.write(Buffer.new(square(lambda, 0.1, 100), FORMAT))
  end
end

Clone this wiki locally