SL Unit 1
SL Unit 1
Unit I
By
Ms S.Rama Devi
Associate Professor
Department of Information Technology
• Because:
• Pure object-oriented language
• Interesting, not entirely obvious implications
• Interesting design decisions (compare Java)
• Particularly type system, mixins, etc.
• Interesting, but not our focus
• Scripting language
• RAILS and other frameworks
• Every value is an object, even simple numeric literals and the values
true, false, and nil (nil is a special value that indicates the absence of
value; it is Ruby’s version of null).
• Comments begin with # in Ruby, and the => arrows in the comments
indicate the value returned by the commented code
Example:
#!/usr/bin/ruby –w
puts "Hello, Ruby!";
# Defining class
class Class1
def print_global
puts "Global variable in Class1 is #$global_variable"
end
end
• Class variables begin with @@ and must be initialized before they
can be used in method definitions. Referencing an uninitialized class
variable produces an error.
• Class variables are shared among descendants of the class or module in which
the class variables are defined.
• Overriding class variables produce warnings with the -w option.
• $ Global variables are prefixed with a dollar sign. Ruby defines a number
of global variables that include other punctuation characters, such as $_
and $-K.
• @ Instance variables are prefixed with a single at sign, and class variables
are prefixed with two at signs.
• ? As a helpful convention, methods that return Boolean values often have
names that end with a question mark.
• UTF- UCS Transformation Format 8 -World Wide Web's most common character encoding.
NOTE: Rake is a native tool for Ruby, similar to Unix's “make”. It is used to handle
administrative commands or tasks, which are stored either in a Rakefile or in a . rake file
• However, in most cases, you’ll use RubyGems to install Ruby libraries for
use in your own programs.
• Since RubyGems enables you to install and manage multiple versions of the
same library, you’ll also need to do some new, RubyGems-specific things
when you require those libraries in your code
Gem packages contain different sets of components. Each component gets placed inside
a dedicated location within the gem bundle.
● Application code;
● Tests;
● Description of dependencies;
● Binaries;
● Relevant Documentation;
● Information regarding the package (gemspec).
|__ Rakefile # 6 *.gemspec file, which has the name of the main
directory, contains all package meta-data, e.g. name,
version, directories etc.
|__ [name].gemspec # 7
Ruby and web: Writing CGI scripts
• Ruby is a general-purpose language; it can't properly be called a web
language at all. Even so, web applications and web tools in general are
among the most common uses of Ruby.
• Not only can you write your own SMTP server, FTP daemon, or Web
server in Ruby, but you can also use Ruby for more usual tasks such as
CGI programming or as a replacement for PHP.
Note: CGI is a standard method used to generate dynamic content on web pages. CGI
stands for Common Gateway Interface and provides an interface between the HTTP
server and programs generating web content.
• The CGI standard was defined by the World Wide Web Consortium
(W3C) and specifies how a program interacts with a Hyper Text
Transfer Protocol
require 'cgi’
cgi = CGI.new
puts cgi.header
puts "<html><body>This is a test</body></html>"
require 'cgi’
cgi = CGI.new
cgi['name'] # => "Zara"
cgi.params['name’] # => ["Zara", "Huma", "Nuha"]
cgi.keys # => ["name"]
cgi.params # => {"name"=>["Zara", "Huma", "Nuha"]}
/test/demo_form.php?name1=value1&name2=value2
• Ruby 1.8 and later comes bundled with WEBrick, a flexible, pure-Ruby
HTTP server toolkit.
• It’s an extensible plug in–based framework that lets you write servers
to handle HTTP requests and responses.
• SOAP lets to write both servers and clients using Web services. By their nature,
these applications can operate both locally and remotely across a network.
• After that callback is set up, make the inverse association: when the
list feels the need to scroll, set the appropriate range in the scrollbar
using TkScrollbar#set.