Forum: Ruby Equivalent to ColdFusion's CFDUMP tag in Ruby on Rails?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-03-15 04:16
(Received via mailing list)
One of ColdFusion's best features is it's CFDUMP tag with awesome
visual representation of data, especially for objects, arrays, etc.
For PHP there is a class called dBug created by Kwaku Otchere
(http://dbug.ospinto.com).  It is basically a port of cfdump to PHP
with an identical visual style.

So I was wondering ... has anyone done this for Ruby on Rails?  Or is
there anything similar?

-Kenric
Scott (Guest)
on 2006-03-15 06:25
(Received via mailing list)
Dont know of anything similar, but you could just:

require 'yaml'

variable = {
    "first"=> "1",
    "second" => nil,
    "third"=> {
        "inner third 1" => nil,
        "inner third 2"=>"yeah"
    },
    "fourth" => nil
}

<pre><%= variable.to_yaml %></pre>

Not nearly as fancy though :)
Scott (Guest)
on 2006-03-15 08:15
(Received via mailing list)
arg....  You made me do it.  It sucks, but it works for basic types.
You must use it with an Array or Hash otherwise you get no headers:


class Object
	def to_html(value = self.to_s);
		return "<span class='RdBug_#{self.class.name}'>#{value}</span>"
	end
end

class String
	def to_html; super(self) end
end

class Symbol
	def to_html; super(':' + self.to_s) end
end

class NilClass
	def to_html; super('nil') end
end

class Hash
	def to_html
		html = "<table cellspacing=2 cellpadding=3
class='RdBug_Hash'><thead><td colspan=2
class='RdBug_HashHeader'>#{self.class}</td></thead>"
		self.each_pair do |k, v|
			html += "<tr><td
class='RdBug_HashKey'>#{k.to_html}</td><td>#{v.to_html}</td></tr>"
		end
		html += "</table>"
		super(html)
	end
end

class Array
	def to_html
		html = "<table cellspacing=2 cellpadding=3
class='RdBug_Array'><thead><td colspan=2
class='RdBug_ArrayHeader'>#{self.class}</td></thead>"
		self.size.times do |index|
			html += "<tr><td
class='RdBug_ArrayKey'>#{index}</td><td>#{self[index]}</td></tr>"
		end
		html += "</table>"
		super(html)
	end
end

variable = {
		"first"=> "1",
		:a23 => "second",
    "third"=> {
        nil => "inner third 1",
        "inner third 2"=>"yeah"
    },
		:values => [1, 2, 3, 4],
    nil => "fourth"
}

Then you can do:

<%= variable.to_html %>

To get output that looks very similar to the dBug stuff for Php.  I
used the styles that were at the link you provided, but I had to change
up the names to fit better:

<style type="text/css">
	table.RdBug_Array,table.RdBug_Object,table.RdBug_Hash,table.RdBug_String,table.RdBug_Symbol
{
		font-family:Verdana, Arial, Helvetica, sans-serif; color:#000000;
font-size:12px;
	}

	.RdBug_ArrayHeader,
	.RdBug_ObjectHeader,
	.RdBug_HashHeader,
	.RdBug_StringCHeader,
	.RdBug_SymbolHeader
		{ font-weight:bold; color:#FFFFFF; }

	/* array */
	table.RdBug_Array { background-color:#006600; }
	table.RdBug_Array td { background-color:#FFFFFF; }
	table.RdBug_Array td.RdBug_ArrayHeader { background-color:#009900; }
	table.RdBug_Array td.RdBug_ArrayKey { background-color:#CCFFCC; }

	/* resource */
	table.RdBug_Hash { background-color:#006600; }
	table.RdBug_Hash td { background-color:#FFFFFF; }
	table.RdBug_Hash td.RdBug_HashHeader { background-color:#009900; }
	table.RdBug_Hash td.RdBug_HashKey { background-color:#CCFFCC; }

</style>
Dave B. (Guest)
on 2006-03-15 12:01
(Received via mailing list)
> One of ColdFusion's best features is it's CFDUMP tag with awesome
> visual representation of data, especially for objects, arrays, etc.
> For PHP there is a class called dBug created by Kwaku Otchere
> (http://dbug.ospinto.com).  It is basically a port of cfdump to PHP
> with an identical visual style.
>
> So I was wondering ... has anyone done this for Ruby on Rails?  Or is
> there anything similar?

<%=debug the_object_you_want_to_inspect %>

Also, ask Rails questions to the Rails list rather than here; you'll get
better answers.

Cheers,
Dave
This topic is locked and can not be replied to.