Monday, March 22, 2010

Ruby Notes


[Output]
"puts" writes to the screen with a carriage return at the end
"print" does the same thing without the carriage return

[Reading from the Console]
$name = STRDIN.gets

[Functions]
def welcome(name)
puts "hi #{name}" # "#{}" evaluate the variable
end


[Call Functions]
welcome("Mike") or welcome "Mike"

Extra arguments are gathered into the last variable if preceded with a "*"
def test(a=1,b=2,*c)

[Variable]
- Global variables start with '$'
- Class variables start with '@@'
- Instance variable start with '@'
- Local variables, method names, and method parameters start with lower case letter
- Class names, module names and constants start with uppercase letter
- Variable are composed of letters, numbers and underscrores
- Method Names may end with:
'?' - imply a boolean operation
'!' - imply something dangareous, like strings being modified
- __END__ on its own line with no white space, are ignored
- Lines between =begin and =end are ignored

[Collections]
- mystuff = %w{tivo nokia ipaq} # make a string array
- select # populates a new array with members which meet a criteria

[Loops]
for i in 1..4

[Regular Expressions]
- /a/ =~ # find first "a" and return position
- %r{a} =~ # same thing as above
- /p.r/ =~ # matches any character

[Blocks]
Blocks are nameless chunks of code that may be passed as an argument to a function.
def whereisit
yield
end
whereisit {puts "where is the money?" }


where is the money?

- each # iterated through each item of a collection
- detect # returns the first item matching a logical expression
- select # returns all items matching a logical expression
- collect # returns an array created by doing the operation on each element
- inject # is the reducer function in Ruby. "inject" loops over an enumerable and performs an operation on each object and returns a single value

[File I/O]
- Read File into a string:
file = File.new("myfile.txt")
mytext = file.read


- Read an entire file into an array of lines
lines = IO.readlines("data.txt")

- Read a file line by line
file = File.open("file.txt")
while line = file.gets
puts line
end
ensure
file.close # ensure that file is close
end

OR
IO.foreach("data.txt") { |line| puts line }


- Read only a few bytes at at time
require 'readbytes'
file = File.new("myfile.txt")
while bytes = file.readbytes(80)
print bytes+"\r\n"
end
file.close

Saturday, March 20, 2010

Dead Horse Bay

Today is beautiful whether. First hiking trip of 2010 was to Far Rockaway with the stop in Dead Horse Bay

Friday, March 19, 2010

Billy Elliot (Broadway)

I saw Billy Elliot Broadway Show yesterday.

The shows include politics, music and dancing. Good show. Recommend

Ruby: Classes vs. Module

"Ruby Modules are similar to classes in that they hold a collection of methods, constants, and other module and class definitions. Modules are defined much like classes are, but the module keyword is used in place of the class keyword. Unlike classes, you cannot create objects based on modules nor can you subclass them; instead, you specify that you want the functionality of a particular module to be added to the functionality of a class, or of a specific object. Modules stand alone; there is no "module hierarchy" of inheritance. Modules is a good place to collect all your constants in a central location."

Require
require and load take strings as their arguments
[require "sinatra"]
[load "bicycle.rb"]


Include
accpet any number of Module objects
[include Enumerable, Comparable]

Every class ia a module, the include method does not allow a class to be included within another class.

Can mix in more than one module in a class. However, a class cannot inherit from more than one class.
Class names tend to be nouns, while module names are often adjectives.

Source #1
Source #2

Wednesday, March 17, 2010

bg, fg, ^Z, ^C

CTRL+Z (or ^Z) - suspend or stopped. To list of jobs and their state by running jobs -l.
CTRL+C (or ^C) - kill the process

bg - background
fg - foreground
jobs -l => lists jobs in background

+ flag on the output of jobs -l indicates the current job and the - flag indicates the previous job.
The %% and %+ names refer to the current job while %- refers to the previous job.

More Info: More Jobs

Tuesday, March 16, 2010

vim remove duplicate lines

g/^\(.*\)$\n\1/d

TR (bash)

Can use tr to remove all carriage returns and Ctrl-z ( ^Z ) characters from a Windows file by entering:

tr -d '\15\32' < winfile.txt > unixfile.txt

tr '\r' '\n' < web_to_print_compare-edited.csv > unixfile.csv


Note: cannot use tr to convert a document from Unix format to Windows

Thursday, March 11, 2010

sort and remove duplicates in file

sort and uniq will quickly and easily remove duplicates, lsit only the dupilcates or only the unique data.

sort myfile.txt | uniq

Wednesday, March 10, 2010

Metaphone or Double Metaphone

Methaphone / Double Metaphone is an algorithm to match words that are spelled differently, but sound the same. Its useful for avoiding duplicated names in a database.

VIM helpful commands

:sort u (remove duplicate line)
:g/pattern/d (delete line)
:g/^$/d (delete blank lines)
:%s/.*/\U&/ (UPPER CASE)
:%s/.*/\L&/ (LOWER CASE)

Friday, March 5, 2010

same origin policy

Aa web page served from domain1.com CANNOT normally connect to or communicate with a server other than domain1.com

POST


  • POST is safer than GET

  • No restrictions in size

  • No Format restrictions. Binary data is also allowed.

  • The browser ussually alerts the user that data need to be resubmitted.

  • multipart/form-data or application/x-www-form-urlencoded

GET


  • GET is less secure compared to POST

  • Data in the URL and URL length is restricted

  • GET requests are re-executed (Back button/re-submit behavior)

  • Only ASCII characters allowed

  • application/x-www-form-urlencoded

Tuesday, March 2, 2010

lads

a male person of any age between early boyhood and maturity.
Dicrionary: Lads