Ruby Talk – An Introduction to




          Premshree Pillai
        premshree@livejournal.com
Scope of Talk
       What this talk is and what it isn’t
       Me, myself
       What is?
       Why use?
       How to? ...
Purpose
What this talk is?
 Get you interested
 Get you started with Ruby
What it isn’t?
 Not a tutorial



Ruby Talk - An...
Who am I?
(or why should you listen to me)

        21/male/single :)
        Technology consultant
        Freelance writ...
History (Ruby’s, not mine)
      Created (in Japan) by Yukihiro
     Matsumoto, popularly called Matz
      Named as “Ruby...
What the heck is Ruby?
      An object-oriented “scripting” language
      As powerful as Perl; simpler, better OO
      T...
Where can you use Ruby?
        System (n/w, RegExps)
        Web programming (using CGI)
        Agents, crawlers
      ...
Upcoming SlideShare
Loading in …5
×

Pré Descobrimento Do Brasil

1,985
-1

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,985
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Pré Descobrimento Do Brasil

  1. 1. Ruby Talk – An Introduction to Premshree Pillai premshree@livejournal.com
  2. 2. Scope of Talk What this talk is and what it isn’t Me, myself What is? Why use? How to? (a quick run through the syntax) Quick comparisons (with Perl and Python) Resources Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 2
  3. 3. Purpose What this talk is? Get you interested Get you started with Ruby What it isn’t? Not a tutorial Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 3
  4. 4. Who am I? (or why should you listen to me) 21/male/single :) Technology consultant Freelance writer since 2001 Perl/Python/Ruby/REBOL hacker Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 4
  5. 5. History (Ruby’s, not mine) Created (in Japan) by Yukihiro Matsumoto, popularly called Matz Named as “Ruby” to reflect its Perl hertitage Released to the public in 1995 Licensed under GPL or Ruby terms Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 5
  6. 6. What the heck is Ruby? An object-oriented “scripting” language As powerful as Perl; simpler, better OO The simplicity of Python Follows the principle of “Least Surprise” – What You Expect Is What You Get Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 6
  7. 7. Where can you use Ruby? System (n/w, RegExps) Web programming (using CGI)  Agents, crawlers DB programming (using DBI) GUI (Tk, RubyMagick) Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 7
  8. 8. General Features High level language True OO (everything’s an object!) Interpreted Portable Low learning curve A quick scan thro’ the syntax Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 8
  9. 9. Running Ruby From the command line: ruby file.rb Windows binary comes bundled with Scintilla Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 9
  10. 10. Basic stuff print 'Hello, world!' p 'Hello, world!' # prints with newline my_var = gets # get input Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 10
  11. 11. Operators + (addition) - (subtraction/negation) * (multiplication) / (division) % (modulus) ** (exponentiation) Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 11
  12. 12. Operators (contd.) == <=> (returns -1, 0 or 1) <, <=, >=, > =~ (matching) eql? (test of equality of type and values) Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 12
  13. 13. Operators (contd.) ++ and -- are not reserved operators Use += and +- Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 13
  14. 14. Logical Operators and or not Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 14
  15. 15. Typing Dynamic typed  Type checking at run-time Strong typed Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 15
  16. 16. Basic Data Types Integers and floats Strings Ranges Arrays Hashes Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 16
  17. 17. Strings my_str = 'whatever' my_str = "blah, blah" my_str.split(",")[0].split("")[2] * 3 Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 17
  18. 18. Ranges Inclusive range my_range = 1 .. 3 my_range = 'abc' .. 'abf' Non-inclusive range my_range = 1 … 5 my_range = 'abc' … 'abf' Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 18
  19. 19. Arrays my_array = [1, 2, 3] Common methods: my_array.length my_array << 4 my_array[0], etc. Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 19
  20. 20. Hashes my_hash = { 'desc' => {'color' => 'blue',}, 1 => [1, 2, 3] } print my_hash['desc']['color'] will return blue Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 20
  21. 21. Hashes (contd.) Common methods: my_hash.keys my_hash.values Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 21
  22. 22. Data Type Conversion Converting to an Array: var_data_type.to_a Converting to an String: var_data_type.to_s More (guess!): var_data_type.to_i var_data_type.to_f Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 22
  23. 23. Everything's an Object Methods can be applied to data directly – not just on variables holding data Example: 5.to_s will return "5" Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 23
  24. 24. Code Blocks Code blocks may use braces ( { } ) or do/end Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 24
  25. 25. Code Blocks (contd.) Example def my_print(what) print what end You cannot use braces for function blocks Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 25
  26. 26. If Statement if expression code block elsif (expression) code block else code block end Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 26
  27. 27. While Statement while expression code block end Example: count = 1 while count < 10 print count count += 1 end Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 27
  28. 28. For Loop for variable_name in range code block end Example: for count in 0..2 print count end Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 28
  29. 29. Iterators array_or_range = value array_or_range.each { |x| print x } Example: my_range = 1..5 my_range.each { |x| print x } Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 29
  30. 30. Functions Functions begin with the keyword def def function_name([args]) code block end Example: def print_name(name='Ruby') print name end Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 30
  31. 31. OO Ruby Classes are containers for static data members and functions Declared using the class keyword. All class names should begin with a capital letter Constructor declared using the initialize keyword Class variables precede with an “@” Objects created using the new method Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 31
  32. 32. Example class class My_class def initialize(arg1, arg2) @arg1 = arg1 @arg2 = arg1 end def print_arg1() print @arg1 end def print_foo() print "I Love Ruby!" end private def print_arg2() print @arg2 end end my_object = My_class.new(2, 3) my_object.print_arg1 my_object.print_arg2 # will cause an exception Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 32
  33. 33. Inheritance class Derived_class < My_class def initialize() @arg = "I Love Ruby!" end def print_arg() print @arg end end my_object = Derived_class.new my_object.print_foo Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 33
  34. 34. Notes on OO Ruby Access specifiers: public, protected, private Multiple inheritance not possible Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 34
  35. 35. Ruby modules require 'net/http' superclass subclass Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 35
  36. 36. Advanced topics Regular expressions Network programming MT programming GUI programming (using Tk) Web programming Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 36
  37. 37. Now what? What you can do now? Get your hands dirty with Ruby Write simple Ruby programs What you have to do? Explore Ruby modules Find a problem, and Ruby it! Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 37
  38. 38. Perl compared to Ruby Complicated OO Cryptic code (Ruby is often called “A Better Perl”) PS: Don’t kill me! Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 38
  39. 39. Python compared to Ruby Incomplete OO Instance variables require self.var No class method No true GC (uses ref counting) Not suitable for one-liners PS: Don’t kill me! Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 39
  40. 40. Resources Ruby Home Page http://www.ruby-lang.org/en/ Programming Ruby http://www.rubycentral.com/book/ RubyGarden http://www.rubygarden.org/ruby Ruby Application Archive (RAA) http://raa.ruby-lang.org/ Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 40
  41. 41. Resources (contd.) RubyForge http://rubyforge.org/ ruby-talk http://blade.nagaokaut.ac.jp/ruby/ruby-talk Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 41
  42. 42. If God did OOP, he’d probably do it in Python; He’s now considering switching to Ruby! Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 42
  43. 43. Thank you! Questions? Ruby Talk - An Introduction to Ruby -- Linux Bangalore/2004 43

×