Skip to content

Instantly share code, notes, and snippets.

class Calculator
attr_accessor :x, :y
def initialize(x, y)
@x, @y = x, y
end
def Calculator.description
string = "Performs basic mathematical operations"
end
class Person
attr_reader :first_name, :age, :gender, :family
def initialize(first_name, age, gender)
@first_name = first_name
@age = age
@gender = gender
@family = nil
end
@eladmeidar
eladmeidar / posts_controller.rb
Created August 25, 2014 16:28
Better Strong Params approach
class PostsController < ApplicationController
# Automatically create the strong parameters required method
def PostsController.filter_parameters(options = {})
options[:for] = [options[:for]] if options[:for].is_a?(Symbol)
options[:for].each do |action_name|
define_method("#{action_name.to_s}_params") do
base = options[:require].keys.first
params.require(base).permit(*options[:require][base])
end
@eladmeidar
eladmeidar / users_controller.rb
Created August 25, 2014 15:35
better strong params
class UsersController < ApplicationController
filter_params require: {user: [:name, :description]}, only: [:create, :update]
def create
end
def update
end
end
redis.watch("running")
if redis.exists("running") == true
puts "already running"
exit
end
redis.multi do
redis.setex("running", 300, true)
end
begin
@eladmeidar
eladmeidar / superdownloader.rb
Created July 27, 2014 14:15
Using yield for configuration blocks
class Configuration
attr_reader :download_url, :speed
def initialize
@download_url = ""
@speed = :fastest
end
def url(new_download_url)
@eladmeidar
eladmeidar / example.rb
Last active August 29, 2015 14:04 — forked from anonymous/example.rb
link "http://api.plnkr.co/catalogue/packages?"
95.times do |i|
page = i + 1
# use page here in the request
end
# ever use while like that again and i'll come over there and kill you
@eladmeidar
eladmeidar / remove_trailing.rb
Created July 8, 2014 10:29
ניקיטה הנסיך
def remove_trailing_zeros(array = [])
if array.size > 0 && array[-1].to_i == 0
array.pop
remove_trailing_zeros(array)
else
array
end
end
@eladmeidar
eladmeidar / miki2prepare.rb
Created May 13, 2014 16:28
Make miki prepare quickly
# encoding: utf-8
require 'rubygems'
require 'skype'
Skype.config :app_name => "Prepare"
120.times do
Skype.message "miki.bergin", "תכין"
end
Fetching git://github.com/radar/better_spree_paypal_express.git
remote: Reusing existing pack: 1284, done.
remote: Total 1284 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1284/1284), 181.85 KiB | 174.00 KiB/s, done.
Resolving deltas: 100% (388/388), done.
Fetching gem metadata from https://rubygems.org/........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Bundler could not find compatible versions for gem "spree_core":
In Gemfile: