Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
#
# Scale RetroArch gamepad overlays to a target screen ratio.
#
# Usage:
#
# $ retroarch-scale-overlay retropad.cfg 19:9 >retropad-19-9.cfg
#
# Usage (all overlays in directory):
#
# frozen_string_literal: true
module Case
extend self
# @param string [#to_s] in kebab or snake case
#
# @return [String]
def to_camel(string)
string.to_s.gsub(/[ -_]+([a-z])/) { $1.upcase }
// https://en.wikipedia.org/wiki/Letter_case#Special_case_styles
import {inspect} from 'util';
export function toCamelCase(string) { // kebab or snake case
return string.replace(/[-_]([a-z])/g, (match, p1) => p1.toUpperCase());
};
export function toKebabCase(camelString) {
return camelString.replace(/[A-Z]/g, match => '-' + match.toLowerCase());
@kj
kj / main.rs
Created November 20, 2019 17:41
use crate::rectangle::Rectangle;
mod rectangle;
fn main() {
let rect1 = Rectangle {
w: 30,
h: 50,
};
let rect2 = Rectangle {
pub struct Rectangle {
pub w: u32,
pub h: u32,
}
impl Rectangle {
/// True if `rect1` fully contains `rect2`.
///
/// # Examples
///
# Alternative to Redis EXPIRE command, to avoid the Redis dependency in simple
# cases where you need an automatically expiring cache.
#
# A cron job or thread can delete expired rows at an interval.
require 'bundler'
require 'json'
Bundler.require
# Doesn't document constructor, slow when using defaults.
class DryInitializerComparison
extend Dry::Initializer
# @return [#call]
option :dep_one, default: -> { DepOne.new }
# @return [#call]
option :dep_two, default: -> { DepTwo.new }
# @return [#convert_values]
option :dep_three, default: -> { DepThree.new }
require 'dry-container'
c = Dry::Container.new
c.register(:a) { Object.new.object_id }
c.register(:b) { |x| Object.new.object_id } # with an arg
puts '-- a'
puts c[:a]
puts c[:a]
AllCops:
Exclude:
- db/schema.rb
TargetRubyVersion: 2.6
Layout/AlignHash:
EnforcedHashRocketStyle: key
Layout/AlignParameters:
EnforcedStyle: with_fixed_indentation

The double splat operator, #to_hash, and dry-auto_inject

I'm having some trouble with the dry-auto_inject Ruby gem when an object that responds to #to_hash (such as Hanami::Entity in my case) is passed as the last argument to .new. When this happens, the generated .new method implicitly converts the object to a hash (because #to_h is for explicit conversion and #to_hash is for implicit conversion, so the double splat operator converts the object), and when passed to #initialize, the injected dependencies and the converted hash are passed altogether as a hash as the first argument.

Example

require 'dry-container'
require 'dry-auto_inject'