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

Inspect a website like a string in Ruby

Put the following function into your ~/.bash_login (or alternatives).

unset -f inspect_url

function inspect_url() { 
   /usr/bin/curl -L -s --max-time 10 "${@}" | ruby -n -e 'p $_.to_s'
   return 0
}

inspect_url http://www.ruby-forum.com

Convert a string to a character array

// converts a string into a character array, then displays the output 1 character at a time from the array.

y = 'testing'.scan(/./)
y.each do |c|
	puts c
end

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

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

Integer to alphanumeric character

A function that converts an integer between 0 and 61 into an alphanumberic character. Ranges from 0-9, A-Z, a-z. Very handy for generating random strings and stuff like that.

function map_char($num) { 
   $int = $num; $int+=48;
   ($int > 57) ? $int += 7 : null;
   ($int > 90) ? $int += 6 : null;
   return chr($int);
}

Inputing non-ascii characters

My mobile phone (6600) doesn't have a Thai input method.
(You can buy a special software to do it)
After a lot of my thought experiments, I got an easy
example from my friend on this. You do a 2-level popup menu
to let people choose from the character table.
from appuifw import *

def thai_input():
    first_list = ['  '.join(thai_char[i:i+11]) for i in range(0,77,11)]
    y = popup_menu(first_list, u'select Thai char')
    if y is not None:
        x = popup_menu(thai_char[11*y:11*(y+1)], u'select Thai char')
        if x is not None:
            t.add(thai_char[11*y + x])

thai_char = [unichr(0x0e01+i) for i in range(77)]   # 77 thai characters

app.body = t = Text()
app.menu = [(u'thai', thai_input), (u'clear screen', t.clear)] 

# wait for user to exit program
import e32      
lock = e32.Ao_lock() 
app.exit_key_handler=lock.signal
lock.wait()


For other language, you can change the unicode offset (0x0e01)
and number of characters (77) to that of your language.
One requirement for this method is that the phone must be
able to display font in your language already.
(I have previously install Thai font on my 6600)
An alternative method to implement this will be manually drawing
your text (combile character from image font file).
I may do that some day.
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS