Skip to content

Instantly share code, notes, and snippets.

A response for:
https://gist.github.com/660194
https://gist.github.com/660968
https://gist.github.com/661951
I don't discuss here whether you shouldn't change the Object class behaviour.
I will write my thoughts on an assumption which it were in ruby standard library.
1. Method name must be shorter at least
class TryChain
attr_reader :obj
def initialize(obj)
@obj = obj
end
def method_missing(sym,*args,&block)
@obj = @obj.respond_to?(sym) ? @obj.send(sym,*args,&block) : nil
self
module Maybe
def wrap_block(block)
lambda do |a|
a==nil ? nil : block.call(a)
end
end
end
module MaybeNumeric
def wrap_block(block)
@ono
ono / bday.irb
Created February 3, 2011 08:32
Kosuke's first program. "Dad, how many days until my birthday?"
kaz:~ kazueono$ irb
>> bday = Time.local 2011,3,19
=> Sat Mar 19 00:00:00 +0000 2011
>> bday.yday
=> 78
>> Time.now
=> Thu Feb 03 08:19:58 +0000 2011
>> Time.now.yday
=> 34
>> bday.yday - Time.now.yday
require 'formula'
class FileMagic <Formula
url 'ftp://ftp.astron.com//pub/file//file-5.04.tar.gz'
homepage 'http://www.darwinsys.com/file/'
md5 'accade81ff1cc774904b47c72c8aeea0'
def install
system "./configure"
system "make install"
@ono
ono / gist:1071586
Created July 8, 2011 10:55
retry_sample.rb
def perform(member_course_id, ls_db, retry_count=0)
member_course = nil
begin
member_course = MemberCourse.find(member_course_id)
rescue ActiveRecord::RecordNotFound => e
if retry_count<3
# Chances are that the job is enqueued before the transaction is
# committed. #6511
Resque.enqueue_at(30.seconds.from_now, self.class, member_course_id, ls_db, retry_count+1)
return
@ono
ono / gist:1071604
Created July 8, 2011 11:06
base_job.rb
# BaseJob class is an abstruct class for all jobs.
# The common operations such as newrelic monitoring should be written here.
# Jobs which is inherited from this class have to override instance method of perform instead of the class method to be monitored.
# Please check hello_world_job.rb as an example.
class BaseJob
# include NewRelic::Agent::Instrumentation::ControllerInstrumentation
def self.skip_mode?
$rails_config && $rails_config.app_config && $rails_config.app_config['resque_skip_queue']
end
@ono
ono / unicorn.conf.rb
Created August 3, 2011 15:49
Workaround to avoid "adding listener failed addr=0.0.0.0:8080 (in use)" error by unicorn on FreeBSD jail host
# If you run unicorn on FreeBSD Jail, you might experience the following error
# when you send some signals such as HUP, USR2 to master process.
# adding listener failed addr=0.0.0.0:8080 (in use)
#
# This snippet gives you a workaround without specifying IP address.
# Returns true if the host is in the Jail.
def in_jail?
`sysctl security.jail.jailed` =~ /security.jail.jailed: 1/ ? true : false
end
@ono
ono / gist:1139622
Created August 11, 2011 13:17
Core Dump
$ gdb ruby18 --core=ruby18.core
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "amd64-marcel-freebsd"...(no debugging symbols found)...
Core was generated by `ruby18'.
Program terminated with signal 6, Aborted.
@ono
ono / gist:1140249
Created August 11, 2011 17:31
Core Dump (where)
(gdb) where
#0 0x0000000800c6dfcc in kill () from /lib/libc.so.7
#1 0x0000000800c6cdcb in abort () from /lib/libc.so.7
#2 0x0000000800672410 in rb_bug () from /usr/local/lib/libruby18.so.18
#3 0x00000008006da23f in sigsegv () from /usr/local/lib/libruby18.so.18
#4 <signal handler called>
#5 0x000000081b7675dc in pthread_mutex_lock () from /lib/libthr.so.3
#6 0x00000008072f3795 in xmlRMutexLock () from /usr/local/lib/libxml2.so.5
#7 0x0000000807342561 in xmlDictReference () from /usr/local/lib/libxml2.so.5
#8 0x0000000807345925 in xmlSAX2StartDocument () from /usr/local/lib/libxml2.so.5