Never been to CodeSnippets 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 (or not, you can keep them private!)

About this user

Oliver Haag www.ohcon.de

check rails version

// A project-specific rails installation (in project_xxx/vendor/rails)
// is preferred against the installed gem

// So the actual Rails Version
Rails::VERSION::STRING


// can differ from the gem version
RAILS_GEM_VERSION

create object fom underscore_syntax

// camelize - from underscore_syntax to Uppercas/ClassSyntax
// constantize - create instance
// (params[:property]) -- read data from web-form


    @property = property_type.camelize.constantize.new(params[:property])

get url / uri in *.rhtml file

// find out the current url / uri in *.rhtml file
// is quite simple with the request object
<% page = request.request_uri %>
page: <%= page %>


// or when different urls mean the same page
<% page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
page: <%= page %>

pluralize, depending on count

// from Agile WebDevelopment with rails
// slightly extendet

def pluralize(count, noun, text)
  case count
  when 0: "There are no #{noun.pluralize} #{text}"
  when 1: "There is one #{noun} #{text}"
  else: "There are #{count} #{noun.pluralize} #{text}"
  end
end

count characters

// description of your code here

class Counter
  
def initialize()
  @characters = Hash.new(0)
end

def read()
  @text = IO.read("text.txt")
end

def count_chars
  @text.each_byte do |ch|
  @characters[ch] +=1
  end
end

def report  
  @characters.each do |key, value|
    puts "#{key.chr} (#{key}) occurs #{value} times"
  end  
end

end

findout browser

// the navigator object stores the browser ...

<script type="text/javascript">
document.write("Your browser is " + navigator.appName);
</script>

avoid visible whitespace in html

// if you want a line break and want to indent your html code
// but the browser makes problems with the whitespace
// make the whitespace a comment

//example
<div id="topimages">
	<image class="left" src="image/left.jpg"><!--
	--><image class="left" src="image/mid.jpg"><!--
	--><image class="right" src="image/right.jpg">
</div><!--topimages-->

IdGenerator in java

// IdGenerator

public class IdGenerator {
	
	private long maxId; 
	
	public IdGenerator(long start) {
		maxId = start;
	}
	
	public long getNextId () {
		return ++maxId;
	}

} // IdGenerator

rewrite extension

// its a redirect [R] and it is is permanent (code is 301)
RewriteEngine On
RewriteBase   /the_directory

RewriteRule  ^(.*).html$ $1.php [R=301]

center text in html

// center horizontally
// text-align:center for internet explorer, margin:auto for firefox

<div style="text-align:center">
  <div style="margin:auto">the text</div>
</div>


// center vertically with table
<table><tr><td valign="middle">the text</td></tr></table>


// or without table (only firefox, .. / not in internet explorer <=6!)
<div style="display:table-cell;	vertical-align: middle">the text</div>