asdf install ruby 3.4.4
mkdir ~/ruby-lsp
class NanoCase | |
class Result | |
UNDEFINED = :undefined | |
attr_reader :type, :data | |
def initialize(type, data) | |
@type = type | |
@data = data | |
end |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'benchmark-ips', '~> 2.11' | |
end | |
class Calc | |
def initialize(a, b) |
class FactoryCollectionFilter | |
def initialize(config) | |
@config = config || {} | |
end | |
def call(filters: {}, data: []) | |
return data if filters.empty? | |
filters.reduce(data) do |filtered, (filter, value)| | |
filter_fn = @config[filter] |
#################################### | |
# Pilares da Orientação a Objetos: # | |
#################################### | |
# 👍 Abstração. | > | |
# 👍 Encapsulamento | > > Composição 🙌 | |
# 👍 Polimorfismo | > | |
# 👎 Herança |
class AssemblyLine | |
CARS_PRODUCED_PER_HOUR = 221.0 | |
def self.calculate_cars_per_hour(speed) | |
speed * CARS_PRODUCED_PER_HOUR | |
end | |
def self.calculate_success_rate(speed) | |
case speed | |
when 1..4 then 1 |
class Mecha | |
private attr_accessor(:states_map, :callbacks, :transitions, :current_state) | |
public :transitions, :current_state | |
def initialize(initial_state:, transitions:) | |
self.states_map = transitions.parameters.select { |(type, _)| type == :keyreq }.to_h { [_2, _2] } | |
self.callbacks = Hash.new { |hash, key| hash[key] = [] } | |
self.transitions = transitions.call(**states_map).transform_values(&:freeze).freeze |
module Susurro | |
def subscribe(handler) | |
subscribers << {name: nil, handler: handler} | |
self | |
end | |
def on(event_name, &handler) | |
subscribers << {name: event_name, handler: handler} |
require 'uri' | |
require 'json' | |
require 'net/http' | |
# https://dog.ceo/dog-api/ | |
class DogCeoAPI | |
class FakeStrategy | |
def get_random | |
{ | |
"message" => "https://images.dog.ceo/breeds/terrier-welsh/lucy.jpg", |
module Kind | |
module Interface | |
module State | |
extend self | |
@current = :enabled | |
def disable! | |
@current = :disabled | |
end |