R uby Talk – An Introduction to Premshree Pillai [email_address]
S cope of Talk <ul><li>What this talk is and what it isn’t </li></ul><ul><li>Me, myself </li></ul><ul><li>What is? </li></...
P urpose <ul><li>What this talk is?   </li></ul><ul><li>Get you interested </li></ul><ul><li>Get you started with Ruby </l...
W ho am I? <ul><li>(or why should you listen to me) </li></ul><ul><li>21/male/single :) </li></ul><ul><li>Technology consu...
H istory (Ruby’s, not mine) <ul><li>Created (in Japan) by Yukihiro Matsumoto, popularly called Matz </li></ul><ul><li>Name...
Upcoming SlideShare
Loading in...5
×

LoteríA Correcta

612

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
612
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

LoteríA Correcta

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

×