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-6 of 6 total  RSS 

Get the name of the script that is running

script-name: does [system/options/script]

Get the currently running method name in Ruby

from: http://www.ruby-forum.com/topic/75258

Author: Robert Klemme



module Kernel
 private
    def this_method_name
      caller[0] =~ /`([^']*)'/ and $1
    end
end


class Foo
 def test_method
   this_method_name
 end
end

puts Foo.new.test_method    # => test_method

Convert string to underscore_name

Converts "My House" to "my_house".
Converts " Peter's nice car " to "peters_nice_car".
Converts "_88" to "88"

function string_to_underscore_name($string)
{
    $string = preg_replace('/[\'"]/', '', $string);
    $string = preg_replace('/[^a-zA-Z0-9]+/', '_', $string);
    $string = trim($string, '_');
    $string = strtolower($string);
    
    return $string;
}

helper to determine if radio/checkbox needs to be checked

I frequently have to use methods such as 'radio_button' and 'check_box_tag' when I don't have an object with a method that will automatically determine the value of the input field. Therefore, I have to check to see if a certain parameter has been passed, and if so, if the parameter's value matches that of the input's value. This method does that.

It's designed to be used in a Rails helper. You can either pass it the object, method, and value (the same parameters as, for example, radio_button) or name and value (the same parameters as radio_button_tag).

def checked?( *args )
  if args.length == 3
    object, method, value = args
    if params[object] && params[object][method] && params[object][method] == value
      'checked'
    end
  elsif args.length == 2
    name, value = args
    if params[name] && params[name] == value
      true
    end
  end
end


Here's an example usage:
<%= radio_button 'person', 'age', '12', :checked => checked?( 'person', 'age', '12' ) %>


If params[:person][:age] exists and it equals '12', then 'checked?' returns 'checked'; otherwise, it returns nil.

HTML 4.0 color names

There are 16 of them
Black  	#000000
Silver 	#C0C0C0
Gray 	#808080
White 	#FFFFFF
Maroon 	#800000
Red 	#FF0000
Purple 	#800080
Fuchsia #FF00FF
Green 	#008000
Lime 	#00FF00
Olive 	#808000
Yellow 	#FFFF00
Navy 	#000080
Blue 	#0000FF
Teal 	#008080
Aqua 	#00FFFF

From this page.

Converting Between Different Naming Convetions

From Sami Hangaslammi's recipe.
import re

def cw2us(x): # capwords to underscore notation
    return re.sub(r'(?<=[a-z])[A-Z]|(?<!^)[A-Z](?=[a-z])', r"_\g<0>", x).lower()

def mc2us(x): # mixed case to underscore notation
    return cw2us(x)

def us2mc(x): # underscore to mixed case notation
    return re.sub(r'_([a-z])', lambda m: (m.group(1).upper()), x)

def us2cw(x): # underscore to capwords notation
    s = us2mc(x)
    return s[0].upper()+s[1:]

Result
>>> cw2us("PrintHTML")
'print_html'
>>> cw2us("IOError")
'io_error'
>>> cw2us("SetXYPosition")
'set_xy_position'
>>> cw2us("GetX")
'get_x'
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS