This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ActiveRecord rails 2.3.9 patch to add an error on the attribute or base otherwise | |
| module ActiveRecord | |
| class Errors | |
| def add_on_attr_or_base(attribute, message = nil, options = {}) | |
| if @base.respond_to?(attribute) | |
| add(attribute, message, options) | |
| else | |
| ActiveRecord::Base.logger.warn "attribute #{attribute} does not exist so we are adding on base" | |
| add_to_base(message) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| t = Thread.new { kill } | |
| puts t.inspect | |
| # irb eval's code. but then this works as a script but does not work in irb | |
| # eval "t = Thread.new { kill }; puts t.inspect" | |
| __END__ | |
| runs successfully when run as a script ie. ruby <filename> | |
| #<Thread:0x1dc5090 dead> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # USAGE: ruby <file.rb> | |
| threads = [] | |
| threads << Thread.new { sleep 10; puts "one" } | |
| # does not work | |
| # /tmp/bar.rb:12: undefined local variable or method `kill' for main:Object (NameError) | |
| # from /tmp/bar.rb:18:in `join' | |
| # from /tmp/bar.rb:18 | |
| # from /tmp/bar.rb:18:in `each' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://github.com/rubygems/rubygems-mirror/blob/master/lib/rubygems/mirror/pool.rb | |
| # also check | |
| require 'thread' | |
| class MirrorPool | |
| def initialize(size) | |
| @size = size | |
| @queue = Queue.new | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| t = Thread.new { sleep 5; puts 1 } | |
| puts "status: #{t.status} on #{t.inspect}" | |
| t.join(1) | |
| puts "status: #{t.status} on #{t.inspect}" | |
| t.join(1) | |
| puts "status: #{t.status} on #{t.inspect}" | |
| t.join(1) | |
| puts "status: #{t.status} on #{t.inspect}" | |
| t.join(1) | |
| puts "status: #{t.status} on #{t.inspect}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # throwing and catching an exception is worse than returning a simple type | |
| # maybe it is because exceptions have to maintain the backtrace | |
| # hypothesis: if the code throwing an exception is nested deep then the perf hit will be larger | |
| # as the backtrace will be bigger | |
| # the java and lisp communities have had discussions on exceptions before. | |
| # The LISP guys are smug about the exception handling in their language. dunno why but they are smug about | |
| # everything. smug lisp weenies... | |
| require 'benchmark' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # asked by dhruvasagar on irc | |
| # http://stackoverflow.com/questions/2547059/delphi-performance-case-versus-if | |
| require 'benchmark' | |
| SO_MUCH = 10_000 | |
| X = 'f' | |
| # 8 cases | |
| Benchmark.bmbm do |x| | |
| x.report("case") do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # is it a bad idea to override ruby core classes | |
| # ex: | |
| # 1. overriding functions which almost do the same thing. breaks the principle of surprise. we override | |
| # Array#size but Array#length is the older definition. | |
| # 2. overriding array so that the default position starts at 1 rather than 0 and other included modules | |
| # like Emumerator#each just work. We can do it but it seems very cumbersome and fragile. | |
| # some good practices given at: http://www.ruby-forum.com/topic/214157 | |
| # do not override core classes, either: | |
| # 1. add it to the metaclass of the specific object |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # goes in an infinite loop. does not work | |
| class Array | |
| def each(&blk) | |
| enum = to_enum | |
| if block_given? | |
| it = enum.next | |
| blk.eval("it=args") | |
| yield | |
| else |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # wget-list: manage the list of downloaded files | |
| # invoke wget-list without arguments | |
| # http://www.linux.com/feature/59457 | |
| # http://ubuntuforums.org/showthread.php?t=672074 | |
| # http://www.lifehack.org/articles/lifehack/make-your-idle-computer-work-for-you.html | |
| # http://decafbad.net/projects/scripts | |
| # http://www.google.co.in/search?q=wget+queue&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a |