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

Measuring the elapsed time

This Ruby code measures how long it takes to display the message "hello world".
   1  
   2  def test_method(statement)
   3    start_time = Time.now
   4    eval(statement)
   5    end_time = Time.now
   6    end_time - start_time
   7  end
   8  
   9  statement = "sleep 1.5; puts 'hello world'"
  10  elapsed_time = test_method(statement)
  11  puts "elapsed time : #{elapsed_time}"

output
   1  
   2  hello world
   3  elapsed time : 1.499266

The eval side of Ruby

The eval method in Ruby executes the code from within a string.
   1  
   2  eval("puts 'Hello World'")

output:
   1  
   2  Hello World

Pull JavaScript code dynamically into your web page at run-time.

Using the JavaScript keyword Eval() with AJAX makes it possible to dynamically add JavaScript code at run-time to your web page.

Here's the complete code dynalert.html, dynalert.js, and dynalert.cgi

file:dynalert.html
   1  
   2  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   3  	"http://www.w3.org/TR/xhtml1-strict.dtd">
   4  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   5  	<head>
   6  		<title>dynalert</title>
   7  		<meta http-equiv="Content-Type" content="type=text/html; charset=ISO-8859-1" />
   8  		<script type="text/javascript" src="dynalert.js"></script>
   9  	</head>
  10  
  11  	<body>
  12  <p id="i1">123 </p><input type="button" value="go" onclick="update()" />
  13  	</body>
  14  </html>


file: dynalert.js
   1  
   2  function populatePage(results){
   3    eval(results);
   4  }
   5  
   6  //send the update
   7  function update() {
   8  
   9  	var strServer = "dynalert.cgi";
  10  	url2 = "";
  11  	SubmitRBData(strServer,"?" + url2);
  12  
  13  }
  14  
  15  function SubmitRBData(strUrl, strPostFields) {
  16  	http.open("GET", strUrl + strPostFields, true);
  17  	http.onreadystatechange = handleHttpResponse;
  18  	http.send(null);
  19  }
  20  function handleHttpResponse() {
  21  
  22  	if (http.readyState == 4) {
  23  		if (http.status == 200)
  24  		{
  25  		results = http.responseText;
  26  		populatePage(results); // string response
  27  		}
  28  	}
  29  
  30  }
  31  
  32  function getHTTPObject() {
  33  
  34  	var objXMLHttp=null
  35  
  36  	if (window.XMLHttpRequest)
  37  	{
  38  		objXMLHttp=new XMLHttpRequest()
  39  	}
  40  	else if (window.ActiveXObject)
  41  	{
  42  		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  43  	}
  44  
  45  return objXMLHttp
  46  
  47  }
  48  
  49  var http = getHTTPObject(); // We create the HTTP Object


file:dynalert.cgi
   1  
   2  #!/usr/bin/ruby
   3  # dynalert.cgi
   4  
   5  require 'cgi'
   6  
   7  cgi = CGI.new
   8  
   9  #title = cgi['title']
  10  
  11  puts "Content-Type: text/html"
  12  puts
  13  puts "function transcript(){ alert('and Bob said this idea might work.');}"
  14  puts "alert('this is a success');"
  15  puts "alert('we can now pass back anything we like');"
  16  puts "transcript();"
  17  puts "oNew = document.createElement('strong');"
  18  puts "oNew.innerHTML = 'yes - this is bold text';"
  19  puts "document.getElementById('i1').appendChild(oNew);"


go from model to associated table name and back

Given a table object, it returns the related string object; e.g. SubAttribute => 'sub-attribute'. Useful if you want to make a list of all your tables with perhaps their fields listed out to the side.

   1  
   2  def stringify_table( table, replace_char = '-', pluralize = false )
   3    string = table.to_s.gsub( /([A-Za-z])([A-Z])/, '\1' << replace_char.to_s << '\2' )
   4    string = string.pluralize if pluralize
   5    string
   6  end


Given a string akin to the name of a table, it returns the related table object; e.g. 'sub_attributes' => SubAttribute.

   1  
   2  def tablify_string( string )
   3    eval( string.to_s.gsub( /_id/, '' ).singularize.split( '_' ).collect { |word| word.capitalize }.join )
   4  end
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS