Skip to content

Instantly share code, notes, and snippets.

View 7even's full-sized avatar

Vsevolod Romashov 7even

View GitHub Profile
COLS = 3 # кол-во столбцов
# если не указан файл, выводим сообщение и выходим
if ARGV.count == 0
puts 'specify a file to output'
exit
end
begin
words = IO.readlines(ARGV[0]).each { |word| word.strip! }.sort
@7even
7even / football results parser
Created April 9, 2011 21:28
football match scores parser
#!/usr/bin/env ruby -wKU
# encoding: utf-8
class FootballParser
require 'rubygems'
require 'nokogiri'
require 'open-uri'
URL = 'http://www.liveresult.ru/'
ITEM_SELECTOR = '#pane_g_172_football_actual .a .item, #pane_g_172_football_actual .b .item'
@7even
7even / hash_struct.rb
Created October 23, 2011 14:44
Struct instantiated with a hash
Kernel.module_eval do
def HashStruct(*attributes)
Class.new do
# keeping keys array in class
@keys = attributes.map(&:to_sym)
class << self
attr_reader :keys
end
# passing keys to the child class if inherited
@7even
7even / gist:2394502
Created April 15, 2012 19:50
number formatting
def separate_with_comma(n)
n.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, '\1,')
end
@7even
7even / gist:2597518
Created May 4, 2012 20:27
репликация новостей с вконтакта
require 'vkontakte_api'
# id группы с минусом
GROUP_ID = -1
# id последней новости, сохраненной в БД
last_news_id = News.maximum(:vk_id)
vk = VK::Client.new
all_news = vk.wall.get(owner_id: GROUP_ID).tap(&:shift).reverse
@7even
7even / gist:3029301
Created July 1, 2012 19:18
method resolving system prototype
require 'hashie'
module VK
module Resolver
NAMESPACES = ['users', 'friends']
def method_missing(method_name, *args, &block)
method_name = method_name.to_s
if NAMESPACES.include?(method_name)
@7even
7even / payment.rb
Created July 9, 2012 18:56
electroenergy payment calculation
# encoding: utf-8
class Payment
RATE = 3.8
attr_accessor :previous, :current
def initialize(params = {})
@previous = params.delete(:previous)
@current = params.delete(:current)
end
module PB
module SocialNetwork
class Twitter
include PB::SocialNetwork::Base
def initialize(token)
@token = token
end
def update_profile(params = {})
@7even
7even / fibonacci.rb
Created September 3, 2012 14:46
Fibonacci sequence via Enumerator
def fibonacci(numbers_count)
sequence = Enumerator.new do |yielder|
current_number, next_number = 0, 1
loop do
yielder << current_number
current_number, next_number = next_number, current_number + next_number
end
end
def defaultify(hash)
hash.default_proc = ->(hash, key) {
hash[key] = Hash.new
}
end
class UsersController < ActiveRecord::Base
def create
defaultify(hash)
# code