Code Snippets Ruby

Short texts Ruby ๐Ÿง‘โ€๐Ÿ’ป Code snippets Ruby โœ๏ธ Reading lists Ruby ๐Ÿ‘€

narou-download.rb

require ‘nokogiri’ require ‘open-uri’ base = ‘https://ncode.syosetu.com’ ncode = ‘x0000xx’ i0 = i = 777 idx = Nokogiri::HTML URI.open(“#{base}/#{ncode}”) p out = `date -Im`.strip `mkdir #{out}` chaps = ” idx.css(‘dd’)[i..].each do |dd| a = dd.at_css ‘a’ href = a[‘href’] i = href[9..-2] # rewrite outer `i` title = “็ฌฌ#{i}็ซ ยท#{a.text}\n” ch = title + Nokogiri::HTML(URI.open(base + […]

pronounced jasopts

require ‘json’ class Cell < Struct.new(:item); end class Args attr_reader :pos, :top def initialize() @pos = [] @top = {} @stack = [Cell.new(@top)] end def current_hash() c = @stack.last return c.item if c.item.is_a?(Hash) return c.item = {} if c.item == nil if c.item.is_a?(Array) h = Cell.new({}) c.item

A basic shopping cart with discounts using a hash.

# frozen_string_literal: true PRICE_RULES = { A: { price: 50, discount_quantity: 2, discount_price: 45 }, B: { price: 30, discount_quantity: 3, discount_price: 25 }, C: { price: 20, discount_quantity: nil, discount_price: nil }, discount: { total_basket: 200, off: 0.1 } }.freeze class Checkout attr_reader :price_rules def initialize(price_rules) @price_rules = price_rules set_default_purchase_quantity end def scan(item) purchases[item] […]

OOP in Ruby without classes

leandro = new_instance(Person, “Leandro”, 26) john = new_instance(Person, “John”, 42) send_message(leandro, :say_hello) # => Hello from Leandro send_message(john, :say_hello) # => Hello from John