Skip to content

Instantly share code, notes, and snippets.

View kopylovvlad's full-sized avatar
💭
I ❤️ Ruby

Kopylov Vladislav kopylovvlad

💭
I ❤️ Ruby
  • RamblerGroup
  • Moscow
View GitHub Profile
require_relative './event_emitter_core'
# class for users
class User
attr_reader :first_name
def initialize(first_name)
@first_name = first_name
end
end
require_relative './event_emitter_core'
class EventEmitter
include EventEmitterCore
end
event_emitter = EventEmitter.new
# the code should be executed when event will be triggered
ring_bell = lambda do |event_name, object|
@kopylovvlad
kopylovvlad / event_emitter_core.rb
Last active April 20, 2019 09:10
EventEmitterCore
# the module can be included to any class
module EventEmitterCore
# turn on the event
# @param event_name [String, Symbol]
def on(event_name)
events[event_name.to_sym] ||= []
end
# turn off the event
# @param event_name [String, Symbol]
Tags
Hash:
Hash<KeyType, ValueType>
Hash{KeyTypes=>ValueTypes}
how to doc hash in arguments
<%
# @param [Hash] opts the options to create a message with.
@kopylovvlad
kopylovvlad / self_printing.rb
Created February 21, 2019 17:42
The script print out its code
$MY_FILE_PATH = File.expand_path(File.dirname(__FILE__))
content = File.open("#{$MY_FILE_PATH}/#{__FILE__}").read
puts content
puts 'end of script'
------------------------------
********************
CONFIDENT RUBY
********************
------------------------------
каждый метод может состоять максимум из 4 частей
1 Collecting input
2 Performing work
3 Delivering output
require 'time'
# thread pool class
# it takes pool size
class ThreadPool
attr_reader :result, :number, :size
def initialize(size)
@finish = false
@result = nil
require 'time'
module HashGenerator
# calculating hash for string "#{string}#{number}"
def self.call(string, number)
# for macOS
return `echo '#{string}#{number}' | sha2 -256`
.gsub('SHA-256 ((null)) = ', '')
.gsub(/\n/, '')
# for Linux
# thread pool class
class ThreadPool
def initialize(size)
@size = size
@jobs = Queue.new
@pool = Array.new(@size) do |i|
Thread.new do
Thread.current[:id] = i
catch(:exit) do
loop do
require 'socket'
# run on localhost:3000
server = TCPServer.new('localhost', 3000)
loop do
Thread.start(server.accept) do |socket|
request = socket.gets
method, path = request.split
sleep_time = rand(5)