Skip to content

Instantly share code, notes, and snippets.

@shime
shime / exceptions.rb
Created April 8, 2012 19:08
defining a lot of exceptions in a module
module Prawn
module Errors
exceptions = %w[ FailedObjectConversion InvalidPageLayout NotOnPage
UnknownFont IncompatibleStringEncoding UnknownOption ]
exceptions.each { |e| const_set(e, Class.new(StandardError)) }
end
end
@shime
shime / struct.rb
Created April 8, 2012 19:10
fun with struct
Contact = Struct.new(:first, :last, :email)
p Contact.new(*%w[James Gray james@grayproductions.net])
# >> #<struct Contact first="James",
# last="Gray",
# email="james@grayproductions.net">
p Contact.new(*%w[James Gray])
# >> #<struct Contact first="James", last="Gray", email=nil>
p Contact.new("James")
# >> #<struct Contact first="James", last=nil, email=nil>
@shime
shime / test.rb
Created April 10, 2012 14:11
Testing flattening of params with airbrake
require 'rubygems'
require 'airbrake'
Airbrake.configure do |config|
config.api_key = '092f2e6780f7c9117353d28dbe8486a3'
config.logger = Logger.new STDOUT
end
params = { 'blah' => 'blah', 'deeper_level' => {'stuff' => 'stuff', 'array' => ['a', 'b', 'c'] } }
@shime
shime / files.rb
Created April 16, 2012 09:30
print all ruby files in current directory
Dir["*.rb"].each { |f| puts f }
@shime
shime / download.rb
Created April 16, 2012 09:51
writting string to file
# downloading content from the web
require 'open-uri'
url = 'http://blog.stackoverflow.com/audio/stackoverflow-podcast-001.mp3'
open(url).read.to_file(url.split('/').last)
# And boom, you’ve downloaded the file to “stackoverflow-podcast-001.mp3”
# napisi program u kojem korisnik unosi neke brojeve od 1 do n i ispisuje zbroj neparnih brojeva
puts "Upisite n:"
n = gets.chomp.to_i # gets => čitanje sa tipkovnice dok ne stisneš enter
# chomp => makni enter s kraja stringa ("\n") - jer smo stisnuli enter i dodali ga
# to_i => pretvara string u broj
# nek sad krene unosit tih n brojeva
brojevi = (1..n).map { gets.chomp.to_i } # <3
@shime
shime / replace.java
Created April 23, 2012 09:11
replace every character at odd index with "&" in a string
class Nes
{
public static void main(String[] args) {
String a = "lastavice trče";
for (int i=0; i < a.length(); i++){
if (i % 2 != 0){
a = a.substring(0,i-1) + "&" + a.substring(i, a.length());
}
}
@shime
shime / tester.rb
Created April 30, 2012 14:59
tester for exceptional
Exceptional::Remote.error(Exceptional::ExceptionData.new(RuntimeError.new, "Test Exception"))
@shime
shime / ZR.rb
Created May 15, 2012 01:29 — forked from DamirSvrtan/ZR
ZR
require 'sinatra'
require 'rubygems'
require 'httparty'
require 'net/http'
require 'base64'
set :dump_errors, false
class Representative
include HTTParty
@shime
shime / hack.rb
Created May 20, 2012 16:21 — forked from rkh/hack.rb
Overriding a class instance method with a module
class Base
def call
'call'
end
end
p Base.new.call #=> "call"
# Monkeypatching "works" but doesn't provide access to #super