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

About this user

James Robertson http://www.r0bertson.co.uk

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

Measuring the elapsed time

This Ruby code measures how long it takes to display the message "hello world".
def test_method(statement)
  start_time = Time.now
  eval(statement)
  end_time = Time.now
  end_time - start_time
end

statement = "sleep 1.5; puts 'hello world'"
elapsed_time = test_method(statement)
puts "elapsed time : #{elapsed_time}"

output
hello world
elapsed time : 1.499266

The eval side of Ruby

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

output:
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>dynalert</title>
		<meta http-equiv="Content-Type" content="type=text/html; charset=ISO-8859-1" />
		<script type="text/javascript" src="dynalert.js"></script>
	</head>

	<body>
<p id="i1">123 </p><input type="button" value="go" onclick="update()" />
	</body>
</html>


file: dynalert.js
function populatePage(results){
  eval(results);
}

//send the update
function update() {

	var strServer = "dynalert.cgi";
	url2 = "";
	SubmitRBData(strServer,"?" + url2);

}

function SubmitRBData(strUrl, strPostFields) {
	http.open("GET", strUrl + strPostFields, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}
function handleHttpResponse() {

	if (http.readyState == 4) {
		if (http.status == 200)
		{
		results = http.responseText;
		populatePage(results); // string response
		}
	}

}

function getHTTPObject() {

	var objXMLHttp=null

	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}

return objXMLHttp

}

var http = getHTTPObject(); // We create the HTTP Object


file:dynalert.cgi
#!/usr/bin/ruby
# dynalert.cgi

require 'cgi'

cgi = CGI.new

#title = cgi['title']

puts "Content-Type: text/html"
puts
puts "function transcript(){ alert('and Bob said this idea might work.');}"
puts "alert('this is a success');"
puts "alert('we can now pass back anything we like');"
puts "transcript();"
puts "oNew = document.createElement('strong');"
puts "oNew.innerHTML = 'yes - this is bold text';"
puts "document.getElementById('i1').appendChild(oNew);"


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