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

Oliver Haag www.ohcon.de

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

convert between characters and values

// character to ASCII value: use ?
?a     # => 97
?\n    # => 10


// string to integer: use []
'a'[0]        # => 97
'hallo'[1]    # => 97


// integer / number to character: use .chr
97.chr     # => "a"
10.chr     # => "\n"


//more info: "Ruby Cookbook", O'Reilly

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])

find out the current 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 %>

// but then different urls mean the same page
// (../admin = ../admin/ = ../admin/index = ..admin/index/)
// and maybe the id is unwanted too (../admin/show/8)
// so below is an alternative with control on which parameter is used

<% page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
page: <%= page %>

redefine [] operator in ruby

// the parameter is written behind the operator in parantheses
// the parameter must be an integer/fixnum
//
// i.e.
// def [](chunk)
// @internal_id_hash[chunk.chunk_id]
// end
// is not possible


class ChunkList
  ..

  def [](chunk_id)
    @internal_id_hash[chunk_id]
  end
end

singleton in ruby

// simple singleton

class Singleton 
  private_class_method :new
  @@singleton = nil
  
  def Singleton.create
    @@singleton = new unless @@singleton
    @@singleton
  end
end

count characters

// Sample Output, "character (byte) occurs n times", 32 = space
// s (115) occurs 3 times
// n (110) occurs 1 times
// ..
// (32) occurs 3 times

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




// usage
count = Counter.new()
count.read
count.count_chars
count.report

test if sites are online

// test if sites are online by title validation
// if the title can change, test can validate occurance of a phrase instead

require 'rwebunit'  # ruby web test based on watir (uses ie=internet-explorer-object)

# test if sites are online by title validation
# usage: to run this test without visible ie use the -b option
# C:\ruby\workspace\ruject1>ruby rwu_site_checker.rb -b

class RwuSiteChecker < RWebUnit::WebTestCase

  # hash with url and title
  @@sites = {
    "http://www.domain_number_one.de" => "title number one",
    "http://www.seccond_domain.org" => "seccond title",
    "http://www.yet_another_domain.com" => "yet another title"
  }
  
  # test for titles
  def test_titles()
    log = "testing title \n"
    @@sites.each { |url, title|
      getTestContext().base_url=url
      beginAt("/")
      assertTitleEquals(title)
      # to check for phrase: assertTextPresent(phrase) 
   
      log += url + " ok \n"
    }
    puts log
  end
  
end

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