Couple of Weeks Back i started reading Beginning Ruby Book By Peter Cooper.
I wanted to share few important quotations i found from the next 3 chapters(10-11-12) from the book.
1) Ruby is an interpreted language, so to distribute Ruby programs you can simply distribute the source code files you’ve written. Anyone else who has Ruby installed can then run the files in the same way that you do.
This process of distributing the actual source code for a program is typically how most programs developed using a scripting language, such as Ruby, are shared, but more traditionally software has been distributed without the source code included. Popular desktop application development languages such as C and C++ are compiled languages
whose source code is converted directly into machine code that runs on a certain platform. This software can be distributed by copying the resulting compiled machine code files, rather than the source, from machine to machine. However, this technique is not possible with Ruby, as there is currently no Ruby compiler available, so you have to distribute your source code in one sense or another for other people to be able to run your programs.
2) Environment Variables
Whenever a program is run on a computer, it’s contained with a certain environment, whether that’s the command line or a GUI. The operating system sets a number of special variables called environment variables that contain information about the environment. They vary by operating system, but can be a good way of detecting things that could be useful in your programs.
You can quickly and easily inspect the environment variables (as supplied by your operating system) on your current machine with irb by using the special ENV hash:
ENV.each {|e| puts e.join(': ') }
3) We can use these environment variables to decide where to store temporary files, or to find out what sort of features your operating system offers, in real time, much as you did with RUBY_PLATFORM:
tmp_dir = '/tmp'
if ENV['OS'] =~ /Windows_NT/
puts "This program is running under Windows NT/2000/XP!"
tmp_dir = ENV['TMP']
elsif ENV['PATH'] =~ /\/usr/
puts "This program has access to a UNIX-style file system!"
else
puts "I cannot figure out what environment I'm running in!"
exit
end
4) Although eval executes code within the current context (or the context supplied with a binding), class_eval, module_eval, and instance_eval can evaluate code within the context of classes, modules, and object instances, respectively. class_eval is ideal for adding methods to a class dynamically:
class Person
end
def add_accessor_to_person(accessor_name)
Person.class_eval %Q{
attr_accessor :#{accessor_name}
}
end
person = Person.new
add_accessor_to_person :name
add_accessor_to_person :gender
person.name = "Peter Cooper"
person.gender = "male"
puts "#{person.name} is #{person.gender}"
In this example you use the add_accessor_to_person method to add accessors dynamically to the Person class. Prior to using the add_accessor_to_person method, neither the name nor gender accessors exist within Person.
Note that the key part of the code, the class_eval method, operates by using string interpolation to create the desired code for Person:
Person.class_eval %Q{
attr_accessor :#{accessor_name}
}
5) Forking is where an instance of a program (a process) duplicates itself, resulting in two processes of that program running concurrently. You can run other programs from this second process by using exec, and the first (parent) process will continue running the original program.
fork is a method provided by the Kernel module that creates a fork of the current process. It returns the child process’s process ID in the parent, but nil in the child process—you can use this to determine which process a script is in. The following example forks the current process into two processes, and only executes the exec command within the child process (the process generated by the fork):
if fork.nil?
exec "ruby some_other_file.rb"
end
puts "This Ruby script now runs alongside some_other_file.rb"
6) Using the Windows API
Microsoft Windows provides an Application Programming Interface (API) that acts as a library of core Windows-related functions for access to the Windows kernel, graphics interface, control library, networking services, and user interface. Ruby’s Win32API library (included in the standard library) gives developers raw access to the Windows API’s features.
require 'Win32API'
title = "My Application"
text = "Hello, world!"
Win32API.new('user32', 'MessageBox', %w{L P P L}, 'I').call(0, text, title, 0)
First, you load the Win32API library into the program, and then you set up some variables with the desired title and contents of the dialog box. Next, you create a reference to the MessageBox function provided by the Windows API, before calling it with your text and title.
The parameters to Win32API.new represent the following
a. The name of the system DLL containing the function you want to access
b. The name of the function you wish to use
c. An array describing the format of each parameter to be passed to the function
d. A character representing the type of data to be returned by the function
In this case, you specify that you want to call the MessageBox function provided by user32.dll, that you’ll be supplying four parameters (a number, two strings, and another number—L represents numbers, P represents strings), and that you expect an integer to be returned (I representing integer).
Once you have the reference to the function, you use the call method to invoke it with the four parameters. In MessageBox’s case, the four parameters represent the following:
a. The reference to a parent window (none in this case)
b. The text to display within the message box
c. The title to use on the message box
d. The type of message box to show (0 being a basic OK button dialog box)
The call method returns an integer that you don’t use in this example, but that will be set to a number representing which button on the dialog box was pressed.
7) Joseph Weizenbaum (Berlin, January 8, 1923 – March 5, 2008) was a German-American author and professor emeritus of computer science at MIT.
Born in Berlin, Germany to Jewish parents, he escaped Nazi Germany in 1935, emigrating with his family to the United States. He started studying mathematics in 1941 in the US, but his studies were interrupted by the war, during which he served in the military. Around 1952 he worked on analog computers, and helped create a digital computer for Wayne State University. In 1956 he worked for General Electric on ERMA, a computer system that introduced the use of the magnetically-encoded fonts imprinted on the bottom border of checks. This allowed automated check processing via Magnetic Ink Character Recognition, and in 1964 took a position at MIT.
Weizenbaum was the creator of the SLIP programming language.
8) Splitting Sentences into Words
class String
def words
scan(/\w[\w\'\-]*/)
end
end
"This is a test of words' capabilities".words
Output:
["This", "is", "a", "test", "of", "words'", "capabilities"]
About the Author
Peter Cooper is a highly experienced Ruby developer and trainer. He manages BigBold (www.bigbold.com), a Ruby training and development company, and has produced many commercial web sites using Ruby on Rails, the Ruby-based web framework. In addition, he created Code Snippets, one of the world’s largest public code repositories, and Congress, an online chat client utilizing Ajax and Ruby on Rails. He also created Feed Digest, a feed distribution service that was recently profiled by Business 2.0 magazine.
In addition to development work, Peter has written professionally about various development techniques and tools, producing over 100 articles. He was coeditor of WebDeveloper.com, and worked on iBoost.com and Webpedia.com.
He lives in Lincolnshire, England, with his girlfriend. In his spare time he enjoys hiking, camping, and exploring mountains.
Tuesday, 16 September 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment