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
require 'socket' | |
# run on localhost:3000 | |
server = TCPServer.new('localhost', 3000) | |
loop do | |
socket = server.accept | |
request = socket.gets | |
method, path = request.split | |
sleep_time = rand(5) |
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
require 'open-uri' | |
urls = [ | |
'https://www.google.com/', | |
'https://www.youtube.com/', | |
'http://www.rubyflow.com', | |
'https://rubyonrails.org/' | |
] | |
# threads array | |
threads = [] |
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
require 'open-uri' | |
urls = [ | |
'https://www.google.com/', | |
'https://www.youtube.com/', | |
'http://www.rubyflow.com', | |
'https://rubyonrails.org/' | |
] | |
urls.each do |url| | |
resp = open(url) | |
puts "#{url} has content length #{resp.read.size} symbols" |
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
# Fibonacci sequence with recursion | |
def fib(number) | |
if number < 0 | |
raise ArgumentError, 'fib(n) defined for number>=0' | |
end | |
return fib(number - 1) + fib(number - 2) if number > 1 | |
number | |
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
def foo(number, index = 1) | |
# limit | |
raise ArgumentError, 'number must be <= 10 000' if number > 10_000 | |
# exit | |
return number.to_s if number == index | |
# step | |
index.to_s + ' ' + foo(number, index + 1) | |
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
puts 'hello' | |
puts '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
# before (legacy model) | |
# app/models/user.rb | |
class User < ActiveRecord::Base | |
validates :name, length: { maximum: 100 } | |
validates :lastname, length: { maximum: 100 } | |
validates :patronymic, length: { maximum: 100 } | |
before_save :capitalize_name!, :if => :name_process_needed? | |
private |
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
# Before | |
module SearchService1 | |
# Metrics/AbcSize: Assignment Branch Condition size for perform is too high. | |
# Metrics/CyclomaticComplexity: Cyclomatic complexity for perform is too high. | |
# Metrics/MethodLength: Method has too many lines. | |
# Metrics/PerceivedComplexity: Perceived complexity for perform is too high. | |
def self.perform(params = {}) | |
scope = ::User.active | |
if params['id'].present? |
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
# before | |
module Main | |
def self.convert(file, format) | |
if format == 'png' | |
PngGenerator.generate(file) | |
elsif format == 'gif' | |
GifGenerator.generate(file) | |
elsif format == 'bmp' | |
BmpGenerator.generate(file) | |
elsif format == 'tiff' |
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
# Before | |
# Metrics/MethodLength: Method has too many lines. [13/10] | |
def foo1(number) | |
if number == 1 | |
'one' | |
elsif number == 2 | |
'two' | |
elsif number == 3 | |
'three' | |
elsif number == 4 |