Ruby Hell Yeah

1,974 views

Published on

Introduction to Ruby basics and Ruby idioms.

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

No Downloads
Views
Total views
1,974
On SlideShare
0
From Embeds
0
Number of Embeds
97
Actions
Shares
0
Downloads
13
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Ruby Hell Yeah

    1. 1. <ul><li>“ A language that doesn’t affect the way you think about programming is not worth knowing” — Alan Perlis </li></ul>
    2. 2. Ruby? HELL YEAH! htttp://syamantics.com
    3. 3. ./toc <ul><li>What is Ruby? </li></ul><ul><li>Installing Ruby </li></ul><ul><li>Writing our first Ruby program </li></ul><ul><li>Ruby v/s PHP Syntax </li></ul><ul><li>What’s so special about Ruby?? </li></ul>
    4. 4. What is Ruby? <ul><li>A dynamic, object-oriented programming language that is way too different than C-type languages. </li></ul><ul><li>Inspired by LISP, Perl, SmallTalk </li></ul><ul><li>Simple, elegant, natural syntax </li></ul><ul><li>Created by Yukihiro Matsumoto, a.k.a. Matz </li></ul>
    5. 5. Installing Ruby <ul><li>Windows </li></ul><ul><li>http://rubyforge.org/frs/?group_id=167 </li></ul><ul><li>Ubuntu </li></ul><ul><li>sudo apt-get install ruby ri irb </li></ul><ul><li>You can even try out ruby online! </li></ul><ul><li>http://tryruby.hobix.com/ </li></ul>
    6. 6. Our First Ruby Program <ul><li>Open Your Interactive Ruby Shell </li></ul><ul><li>Open up Command Prompt/Shell/Terminal and type irb , then hit enter. </li></ul><ul><li>Now enter this, </li></ul><ul><li>puts &quot;Hello World!&quot; </li></ul>
    7. 7. Our Second Ruby Program <ul><ul><ul><li># The Greeter class </li></ul></ul></ul><ul><ul><li>class Greeter </li></ul></ul><ul><ul><li>def initialize (name = &quot;World&quot; ) </li></ul></ul><ul><ul><li>@name = name </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>def say_hi </li></ul></ul><ul><ul><li>puts &quot;Hi #{ @name } !&quot; </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>def say_bye </li></ul></ul><ul><ul><li>puts &quot;Bye #{ @name } , see you next week.&quot; </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><ul><li># Initialize our Greeter </li></ul></ul></ul><ul><ul><li>g = Greeter.new &quot;Trippert Tech Session“ </li></ul></ul><ul><ul><ul><li># Say hi </li></ul></ul></ul><ul><ul><ul><li>g .say_hi </li></ul></ul></ul><ul><ul><ul><li># Say bye </li></ul></ul></ul><ul><ul><ul><li>g .say_bye </li></ul></ul></ul>
    8. 8. Ruby v/s PHP <ul><li>Well, there are number of differences! </li></ul><ul><li>Ruby is fully OOP, PHP is not. </li></ul><ul><li>Ruby has got no primitive type, PHP has. </li></ul><ul><li>In Ruby, classes are objects, In PHP, classes are classes. </li></ul><ul><li>PHP function calls are Ruby messages. </li></ul><ul><li>Ruby uses sigil denotes for variable scoping. </li></ul><ul><li>Ruby has closures, anonymous functions, PHP does not*. </li></ul><ul><li>… . And many many many more. </li></ul>There’s a create_function() function in PHP to create anonymous function though 
    9. 9. Ruby Syntax : Branches <ul><li>if-else </li></ul><ul><li>switch case </li></ul><ul><ul><li>if xtra_large </li></ul></ul><ul><ul><li>do_xtra_large </li></ul></ul><ul><ul><li>elsif large </li></ul></ul><ul><ul><li>do_large </li></ul></ul><ul><ul><li>elsif medium </li></ul></ul><ul><ul><li>do_medium </li></ul></ul><ul><ul><li>else #small </li></ul></ul><ul><ul><li>do_small </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><ul><li>do_xtra_large if xtra_large </li></ul></ul>ending if
    10. 10. Ruby Syntax : Loops <ul><li>for in </li></ul><ul><li>Iterators </li></ul><ul><li>for i in 1. .. 10 </li></ul><ul><li>print i , &quot; Hello &quot; ; </li></ul><ul><li>end </li></ul><ul><li>projects = [ &quot;Mobracer&quot; , &quot;GodFatherII&quot; , &quot;Tank&quot; , &quot;Madden Quiz&quot; ] </li></ul><ul><li>projects .each do |project| </li></ul><ul><li>puts project </li></ul><ul><li>end </li></ul>while i = 5 while i > 0 puts i i -= 1 end
    11. 11. Ruby Syntax : Containers <ul><li>Arrays </li></ul><ul><li>Hashes </li></ul><ul><li>my_array = Array.new </li></ul><ul><li>my_array = [] </li></ul><ul><li>my_array .push &quot;One&quot; </li></ul><ul><li>my_array .push &quot;Two&quot; </li></ul><ul><li>my_array = [ &quot;One&quot; , &quot;Two&quot; , </li></ul><ul><li>&quot;Three&quot; , &quot;Four&quot; , &quot;Five&quot; ] </li></ul><ul><li>new_array = my_array[ 1. . 3 ] </li></ul><ul><li>new_array = my_array[ 1 , 3 ] </li></ul><ul><li>people = { </li></ul><ul><li>&quot;linus&quot; =>{ &quot;lname&quot; => &quot;Torvalds&quot; , &quot;fname&quot; => &quot;Linus&quot; , &quot;project&quot; => &quot;Linux&quot; }, </li></ul><ul><li>&quot;matz&quot; =>{ &quot;lname&quot; => &quot;Matsumoto&quot; , &quot;fname&quot; => &quot;Yukihiro&quot; , &quot;project&quot; => &quot;Ruby&quot; }, </li></ul><ul><li>&quot;DHH&quot; =>{ &quot;lname&quot; => &quot;Hanmer Heinsen&quot; , &quot;fname&quot; => &quot;David&quot; , &quot;project&quot; => &quot;RoR&quot; }, </li></ul><ul><li>&quot;nate&quot; =>{ &quot;lname&quot; => &quot;Abel&quot; , &quot;fname&quot; => &quot;Nate&quot; , &quot;project&quot; => &quot;CakePHP&quot; } </li></ul><ul><li>} </li></ul><ul><li>people .select do |nick, info| </li></ul><ul><li>puts nick </li></ul><ul><li>info .select do |key, value| </li></ul><ul><li>puts key , value </li></ul><ul><li>end </li></ul><ul><li>end </li></ul>
    12. 12. Ruby Syntax : Subroutines, Modules <ul><li>Subroutines </li></ul><ul><li>Modules </li></ul><ul><li>def doubleMe ( number ) </li></ul><ul><li>number * 2 </li></ul><ul><li>end </li></ul><ul><li>puts doubleMe 3 </li></ul><ul><li>module MyMath </li></ul><ul><li>def doubleMe ( number ) </li></ul><ul><li>number * 2 </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>include MyMath </li></ul><ul><li>puts doubleMe 3 </li></ul>module MyMath TWO = 2 def MyMath .doubleMe( number ) number * TWO end end include MyMath puts MyMath .doubleMe 3 Modules
    13. 13. Ruby Syntax : Classes and Objects <ul><li>Classes & Objects </li></ul><ul><li>class MyMath </li></ul><ul><li>def initialize </li></ul><ul><li>@two = 2 </li></ul><ul><li>end </li></ul><ul><li>def doubleMe (number) </li></ul><ul><li>number * @two </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>my_math = MyMath.new </li></ul><ul><li>puts my_math .doubleMe 4 </li></ul>
    14. 14. Ruby Syntax : Inheritance <ul><li>Extending A Class </li></ul><ul><li>Including Modules </li></ul><ul><li>class MyParentMath </li></ul><ul><li>def initialize </li></ul><ul><li>@two = 2 </li></ul><ul><li>end </li></ul><ul><li>def doubleMe (number) </li></ul><ul><li>number * @two </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>class MyChildMath < MyParentMath </li></ul><ul><li>def quadropoleMe (number) </li></ul><ul><li>doubleMe (number) * doubleMe(number) </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>my_math = MyChildMath.new </li></ul><ul><li>puts my_math .quadropoleMe 4 </li></ul><ul><li>module MyMathModule </li></ul><ul><li>TWO = 2 </li></ul><ul><li>def doubleMe (number) </li></ul><ul><li>number * TWO </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>class MyChildMath </li></ul><ul><li>include MyMathModule </li></ul><ul><li>def quadropoleMe (number) </li></ul><ul><li>doubleMe (number) * doubleMe(number) </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>my_math = MyChildMath.new </li></ul><ul><li>puts my_math .quadropoleMe 4 </li></ul>
    15. 15. Ruby Syntax : Conventions <ul><li>ClassNames </li></ul><ul><li>method_names and variable_names </li></ul><ul><li>methods_asking_a_question? </li></ul><ul><li>slightly_dangerous_methods! </li></ul><ul><li>@instance_variables </li></ul><ul><li>$global_variables </li></ul><ul><li>local_variable </li></ul><ul><li>SOME_CONSTANTS or OtherConstants </li></ul>
    16. 16. What’s so special about Ruby?? <ul><li>Introspection Close to Natural Language Method Redefinition Mixins </li></ul><ul><li>Implicit Singleton methods Meta-programming Dynamic typing </li></ul><ul><li>No Primitives Yield Powerful Fully Object Oriented </li></ul><ul><li>Flexible Symbols Minimalism DRY method_missing </li></ul><ul><li>Hooks Anonymous function Modules Multiple Inheritence Blocks </li></ul><ul><li>Reflection Closures Open objects Classes are objects </li></ul><ul><li>Dynamic Operator Overloading Open classes Sigil Denotes </li></ul><ul><li>Eval Simplicity Optional Parentheses Iterators attr_accessor </li></ul><ul><li>Syntactic Sugar Optional Punctuation Write Less Do More </li></ul>
    17. 17. <ul><li>TBC... </li></ul>

    ×