Skip to content

Instantly share code, notes, and snippets.

@Olefine
Olefine / ru.yml
Created August 5, 2013 12:02
localize for russian
ru:
date:
abbr_day_names:
- Вс
- Пн
- Вт
- Ср
- Чт
- Пт
- Сб
@Olefine
Olefine / append.rb
Created July 20, 2013 09:30
append something into middle of file
def append_config_to_file
tempfile = File.open("file.rb", 'w')
f = File.new("config/application.rb")
f.each do |line|
tempfile << line
if line.match(/class Application/)
tempfile << " config.autoload_paths += %W(\#{config.root}/searches)\n"
end
end
f.close
@Olefine
Olefine / deploy.rb
Created July 12, 2013 07:52
tasks for faye server with thin
namespace :faye_server do
desc "Start faye server"
task :start do
run "cd #{current_path};RAILS_ENV=production bundle exec rackup faye.ru -s thin -E production -D -P tmp/pids/faye.pid"
end
desc "Stop faye server"
task :stop do
run "cd #{current_path};if [ -f tmp/pids/faye.pid ] && [ -e /proc/$(cat tmp/pids/faye.pid) ]; then kill -9 `cat tmp/pids/faye.pid`; fi"
end
@Olefine
Olefine / user.rb
Created July 9, 2013 10:55
user model
class User < ActiveRecord::Base
#default_scope where(:banned => false)
scope :unbanned, where(:banned => false)
include ChooseCity
rolify
acts_as_voter
has_karma(:questions, :as => :submitter, :weight => 0.5)
mount_uploader :avatar, AvatarUploader
@Olefine
Olefine / metaprogrammin.rb
Created June 26, 2013 13:03
2 ways define method
class Module
def access(*args)
args.each do |arg|
instance_var = ("@" + arg.to_s)
define_method(arg.to_s + '=') {|val| instance_variable_set(instance_var, val)}
define_method(arg) { instance_variable_get(instance_var)}
end
end
def attr_access(*args)
@Olefine
Olefine / few2last.rb
Created May 27, 2013 09:50
last elemnts in array using slice and join into string using |
def few2last(array)
array.slice(-2..-1).join("|")
end
@Olefine
Olefine / zip.rb
Created May 27, 2013 09:45
zip arrays
(1..5).to_a.zip (5..10).to_a
#=> [[1, 5], [2, 6], [3, 7], [4, 8], [5, 9]]
@Olefine
Olefine / class_method.rb
Created May 24, 2013 13:54
another way create class methods in ruby
class Item
class << self
def show
puts "Class method show invoked"
end
end
end
@Olefine
Olefine / lambda
Last active December 17, 2015 16:58
lambda expiriments
l = lambda do |string|
if string == "try"
return "There's no such thing"
else
return "Do or do not."
end
end
puts l.call("try")
@Olefine
Olefine / json
Created May 21, 2013 11:14
json ajax rz2
$(document).ready ->
$('div#reload_form form').submit (event) ->
event.preventDefault()
form = $(this)
url = form.attr('action')
ammo = $('#ammo_to_reload').val()
$.ajax
type: 'put'
url: url