Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Ruby - "meta method" execute

//
//
// attempt to execute a method indirectly, I don't know if
// it's possible, but I suspect it is, and probably just
// have the syntax wrong
//

ar = %w(apples bananas oranges)

print "\n ar.class = #{ar.class}"

print "\n ar.methods = #{ar.methods.sort}"
puts "======================================================="

mets = ar.methods.sort

#
# this doesn't work ... is there a syntax for doing this?
#                       i.e. calling a 'meta' method ?
#
# call each method of 'ar' (an array)
#
#

mets.each {|method| ar.method} 

Get the name of the current method in Ruby

Found at http://nubyonrails.com/articles/2006/08/04/seattle-rbbq

def method_name
  if  /`(.*)'/.match(caller.first)
    return $1
  end
  nil
end

def blah
  puts method_name
end

blah  # => 'blah'

Class-Level Method Chaining Using extend instead of include/ClassMethod module

This post is in response to the example of method chaining given at My hovercraft is full of eels.

module ForeignKeyMigrations::Schema

  def self.extended(object)
    class << object
      alias_method :define_without_fk, :define unless method_defined?(:define_without_fk)
      alias_method :define, :define_with_fk
    end 
  end

  def define_with_fk
    ...
    define_without_fk(...)
    ...
  end

end

ActiveRecord::Schema.extend(ForeignKeyMigrations::Schema)


The code I used to test this technique, which is really the same thing with domain information removed:

module Foo
  def self.extended(object)
    class << object
      alias_method :to_s_without_foo, :to_s unless method_defined?(:to_s_without_foo)
      alias_method :to_s, :to_s_with_foo
    end 
  end

  def to_s_with_foo
    "#{to_s_without_foo} - got foo!"
  end

end

class X
end

X.extend(Foo)
puts X.to_s

Execute arbitary SQL in JDBC & get column names etc from the meta data

This will execute an arbitary SQL string in JDBC and extract the column names. I extracted this code from a much larger module I wrote years ago, so it isn't complete and hasn't been tested. You have been warned.

    /*
    ** connection is a java.sql.Connection obtained in the usual way.
    */

    PreparedStatement statement =
	connection.prepareStatement (sqlString);
    statement.setMaxRows (configModel.getMaxRows ());

    if (statement.execute ())
    {
	ResultSet resultSet = statement.getResultSet ();
	ResultSetMetaData metaData = resultSet.getMetaData ();

	/*
	** Get the column names.
	*/

	for (int i = 0 ; i < metaData.getColumnCount () ; i++)
	{
	    int columnType = metaData.getColumnType (i + 1);
	    String columnName = metaData.getColumnLabel (i + 1);

	    /*
	    ** Do something with columnType & columnName.
	    */

	}

	/*
	** Fetch the rows.
	*/

	while (resultSet.next ())
	{
	    String value;

	    for (int i = 0 ; i < metaData.getColumnCount () ; i++)
		value = resultSet.getString (i + 1);
	}
    }
    else
    {
	/*
	** Query was probably update/insert/delete.
	*/

	int rowCount = statement.getUpdateCount ();
    }

    statement.close ();

FileMetaData: simple generic file_info metadata comment

This is a general-purpose default text snippet that
you can use to add metadata to any file. It is based
on YAML.
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : ___filename___
    ###     desc : |
    ###         ___description___
    ###     date : created="___date___"
    ###     last : lastmod="___last___"
    ###     lang : ___lang___
    ###     tags : ___tags___
    ### </region-file_info>
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS