Skip to content

Instantly share code, notes, and snippets.

View deepak's full-sized avatar

Deepak Kannan deepak

View GitHub Profile
@deepak
deepak / jeg2_rmu.irc_chat.transcript.txt
Created January 25, 2011 05:59
some notes of the discussion with JEG2 on #rmu irc channel at http://ietherpad.com/x9mkM1v
# copy of http://ietherpad.com/x9mkM1vS4q
# some notes of the discussion with JEG2 on #rmu irc channel at http://ietherpad.com/x9mkM1vS4q
Blogs
- Dave Thomas ( http://pragdave.pragprog.com/ )
- Jim Weirich ( http://onestepback.org/ )
- James Edward Gray II ( http://blog.grayproductions.net/ )
- Aaron Patterson ( http://tenderlovemaking.com/ )
- timeless ( https://github.com/judofyr/timeless )
@deepak
deepak / mysqldump_csv.sh
Created February 2, 2011 08:37
bash function to takes an arbitrary sql string and a connection (user and pass) and its params and returns the csv output
# takes an arbitrary sql string and a connection (user and pass) and its params
# and returns the csv output
# $1 => sql string (required)
# $2 => params for mysql command other than -u ie.user and -p ie.password (optional)
# $3 => mysql connection command giving user and password (optional)
# tested on mysql.
# TODO:
# 1. remove 'INTO OUTFILE' sql and convert using sed from tsv to csv.
# also use raw mode and see how it escapes comma and tabs. another benefit is that if the
# mysql runs remotely we will not be able to cat the file
@deepak
deepak / multi_line_margin.rb
Created February 3, 2011 07:00
multi_line strings in riby with a margin to have a handle on whitespace eg. indent
#http://stackoverflow.com/questions/1552598/building-multi-line-strings-programatically-in-ruby
# multi_line strings in ruby with a margin to have a handle on whitespace eg. indent
# TODO: rewrite it to use gsub and a single regex eg. code = <<-CODE.gsub(/^\s+/,'');
# quite a lot of the temporary strings are created
# hal fulton author of "the ruby way"
# http://www.informit.com/articles/article.aspx?p=683059&seqNum=3
str = <<end.margin
|This here-document has a "left margin"
|at the vertical bar on each line.
@deepak
deepak / multiline_string_margin.rb
Created February 3, 2011 07:38
markup multilines in a string so as to denote where we want the line to begin explicitly, can nest them too
# markup multilines in a string so as to denote where we want the line to begin explicitly,
# we can nest them too for "indentation"
# inspired from an article by hal fulton author of "The Ruby Way"
# http://www.informit.com/articles/article.aspx?p=683059&seqNum=3
# gisted at https://gist.github.com/809145
# String#margin was splitting the string by creating temporary string while this use a regex
class String
# usage
@deepak
deepak / rakefile_extract_fixtures.rb
Created February 3, 2011 08:59
generate fixtures from actual data in database
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
@deepak
deepak / attr_list.rb
Created March 4, 2011 10:01
get the attributes after the class is loaded
# get the attributes after the class is loaded
# tried two approaches first, overrding :attr_accessor, :attr_reader, :attr_writer is safest
# it works but the method has to patched. if a class already has a patched defination then might have to use
# method-chaining ie. alias. Patching is messy (sometimes) and politically incendiary :-)
# Secondly, tried matching method-names to instance-variables where it if of the pattern
# method-named foo for instance-variable @foo
# but the instance variable is defined only when the variable is defined for
# the first time, even if nil
# this seems cleaner, but has two problems, it does not work and it is not perfect. we might have a
# method-named foo for instance-variable @bar. This problem also exists in the first method
@deepak
deepak / rails_html_helper.txt
Created March 9, 2011 06:30
advantages of rails html helper and rendering of whitespace in erb templates
advantages of rails html helper and rendering of whitespace in erb templates
== code
<div>
<textarea rows='300' cols='30' style="background-color:#DDDDFF">
<%= "hello\nworld" %>
</textarea>
</div>
== html.
@deepak
deepak / regex_scan_offset.rb
Created March 10, 2011 09:47
scan a string, yielding the offset matched
# scan a string, yielding the offset matched
# http://www.rubular.com/r/qCz3jnyfZs
require 'pp'
class String
def scan_offset(regex, old_offset=0, &block)
regex = Regexp.new(regex)
match = regex.match(self)
@deepak
deepak / iterator_delete.rb
Created March 14, 2011 15:06
can we delete elements while iterating
# can we delete elements while iterating
# what about external iterators
# usually have seen that the array is cloned and the delete operation
# done on a copy of the array, this maybe because we are using an internal iterator
require 'pp'
foo = [*'1'..'10'].map!(&:to_i)
pp "before:"
pp foo
@deepak
deepak / rails_nonclosed_input_tag.html.erb
Created March 15, 2011 10:53
The input element in rails formhelper always closes the tag, any way to cleanly override it?
The input element in rails formhelper always closes the tag
Is it possible to have some text or image enclosed in an input tag, and to do it cleanly
what is the rails way to do this?
do we lose anything like, a closing tag with a minus to remove trailing spaces <-%>
<% form_for(@message) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :enabled, "Enabled (Y/N)" %></br>
<%= f.radio_button :enabled, "yes", :checked => true %>Yes</input>