This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KNOWN_PLANINDROMES = %w(racecar hannah).freeze | |
def unique_palindrome(books) | |
palindromes_per_book = {} | |
books.each do |book_name, book_words| | |
palindromes_per_book[book_name] = {} | |
# filter out any unknown palindromes | |
book_words.split(" ").each do |word| | |
next unless KNOWN_PLANINDROMES.include?(word) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Benchmarkable | |
extend ActiveSupport::Concern | |
class_methods do | |
attr_reader :benchmarkable_file_path | |
def benchmark_to(file_path) | |
@benchmarkable_file_path = file_path | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SomeCollection.collection.aggregate([ | |
{ | |
"$match": { # This ensures that we only calculate a count where the attribute exists | |
"some_attribute": { | |
"$exists" => true | |
} | |
} | |
}, | |
{ | |
"$project": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Resources used to create this query | |
# https://docs.mongodb.com/ruby-driver/current/tutorials/ruby-driver-crud-operations/#updating | |
# https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/ | |
ProductCollection.with(client: :background) do |klass| | |
klass.collection.update_many( | |
{"product_references" => {"$exists" => true}}, # ensure that the `product_references` field exists | |
{ "$set" => { "product_references.$[elem].listed" => true } }, # says "update all documents matching the array filters below" | |
{ | |
# this `array_filters` allows you to set conditions on a field that is an array. The "elem" name is arbitrary and only needs to be the same everywhere it is used |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Blah | |
def initialize | |
@hello = 'world' | |
end | |
end | |
blah = Blah.new | |
blah_binding = blah.instance_eval { binding } | |
ERB.new("<%= hello %>").result(blah_binding) | |
# Result: | |
# NameError: undefined local variable or method `hello' for #<Blah:0x00007fcb999d2990 @hello="world"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
anchor = proc do |a| | |
proc {|b| "#{a} #{b}" } | |
end | |
append = proc {|p| p.call("b")} | |
(anchor >> append).call("a") | |
anchor.call("a").call("b") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const form = document.getElementById("form"); | |
form.addEventListener("click", (e) => { | |
e.preventDefault(); | |
const formData = new FormData(form); | |
const csrf = document.querySelector("meta[name='csrf-token']").getAttribute("content"); | |
fetch("/", { | |
method: "POST", | |
body: JSON.stringify({order_form: formData}), | |
headers: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= render StimulusComponent.new controller: "test" do |component| %> | |
<button <%= component.action :click, :test, :test %>>Hello, Stimulus Component!</button> | |
<% end %> |