creinken
4/26/2020 - 4:12 AM

Ruby snips

Collection of useful Ruby snippets

class ClassName
    #### Attributes ####
    
    
    #### Instance Methods ####
    def initialize
        
    end
    
    
    #### Class Methods ####
end
sql = <<-SQL
        # sql goes here
        SQL
class ClassName < ActiveRecord::Base

end
# ruby equivalant of a curl with form data
require 'net/HTTP'

uri = URI.parse("URI goes here")
        request = Net::HTTP::Post.new(uri)
        request.basic_auth("field 1 goes here", "field 2 goes here")
        request.set_form_data(
          "key" => "value",
        )

        req_options = {
          use_ssl: uri.scheme == "https",
        }

        response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
          http.request(request)
        end
# basic ruby class with some commonly used methods

class ClassName
    #### Attributes ####
    @@all = []
    
    
    #### Instance Methods ####
    def initialize
        
    end
    
    def save
        self.class.all << self
    end
    
    
    #### Class Methods ####
    def self.all
        @@all
    end
    
    def self.create
        new_class = self.new
        new_class.save
        new_class
    end
end