Intro To Ruby


Published on

Ruby for Programmers, an introduction that includes conditionals, iterators and blocks

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Conditionals are key to being able to make decisions in a programleft looks like every other languageparentheses are optional in ruby make sure to do ==, = is an assignment, == is a conditional testExplain putsright is a little different…people in ruby don’t like to type…english readableA statement modifier lets you move control structures at the end of an expression.
  • Unlike some languages with the 0 and empty string! Binds more tightly than than the “not” keyword so you do need parentheses for example 1, but don’t need parentheses for example 2
  • Unless can be awkward, especially with else. Usually you’ll want to use if for conditionals with else clauses.Occasionally unless is more readable:unless something is nil
  • Alot of the time you will be using an array when you iterate over somethingAn array is just a list of items.Every spot in the list acts like a variable and you can make each spot point to a different objectW means wordsArray is a class, needs to start with capital letter
  • IRBif you go off the array it will be nil
  • Does anyone know what a hash is? associative array collection of key-value pairskeys can be numbers or strings Difference from an Array
  • merge takes the value from the second hashmerge! changes h1
  • you would think that delete should need a bang to change the hash, but delete doesn’t exist with a bangdelete returns the value
  • 5 is an object that is an instance of the integer classtimes is a method of the 5 objecttimes is a method on an object that is an instance of integer
  • it does the block of code three timesit is very rare that you will see a while loop in can do the loops we did earlier, but rubyists will mock you.
  • What is a block? It is the ability to take a block of code, wrap it up in an object and pass it to a method. Then you can run the block of code within the method any time you want…sometimes twice! The result is kind of like sending a method to a method, except that a block isn’t bound to an object like a method is – it is an object. So what? Why use blocks?elegant syntax for iteratorsBecause there are some things that only blocks can do, like being passed to a method and being returned by a method.
  • Intro To Ruby

    1. 1. Ruby<br />
    2. 2. The Ruby Language<br />Originally by Yukihiro &quot;Matz&quot; Matsumoto<br />“Ruby is designed for programmer productivity and fun, following the principles of good user interface design. He stresses that systems design needs to emphasize human, rather than computer, needs.”<br /><br />Ruby 1.0 was released in 1996. <br />
    3. 3. Ruby Language Overview<br />Dynamically typed<br />Interpreted<br />Can be modified at runtime<br />Object oriented<br />Blocks & lambdas<br />Nice support for Regular Expressions <br />
    4. 4. Lets get started<br />IRB: InteractiveRuBy<br />&gt;&gt; 4<br />&gt;&gt; 4 + 4<br />
    5. 5. Everything is an object<br />“test”.upcase<br />“test”.class<br />“test”.methods<br />
    6. 6. Everything evaluates to something<br />2 + 2<br />(2+2).zero?<br />
    7. 7. Methods are Messages<br /><br /> 4<br />thing.send “do”, 4<br />
    8. 8. Operators are Methods<br /> 4<br /><br />thing.send “do”, 4<br />1 + 2<br />1.+(2)<br />1.send &quot;+&quot;, 2<br />
    9. 9. Write your first Ruby class<br />
    10. 10. Conditionals, Iterators & Blocks<br />With Hashes & Arrays<br />
    11. 11. Conditionals<br />
    12. 12. Conditionals: if<br />if age &gt; 17<br /> puts “can vote”<br />end<br />if age &gt; 17<br /> puts “can vote”<br />else<br /> puts “attends school”<br />end<br />Statement Modifiers:<br />y = 7 if x == 4<br />Other Syntax:<br />if x == 4 then y = 7 end <br />
    13. 13. Truth<br />Truth: Everything is true except for:<br />false <br />nil<br />Therefore<br />0 is true<br />“” is true<br />Checking for false:<br />if !(name == “superman”) …<br />if not (name == “superman”) …<br />
    14. 14. Unless<br />“unless” provides us with another way of checking if a condition is false:<br />unless superpower == nil<br /> status = “superhero”<br />end<br />
    15. 15. Case<br />case superhero<br />when “superman”<br /> city = “metropolis”<br />when “batman”<br /> city = “gotham_city”<br />else <br /> city = “central_city”<br />end<br />
    16. 16. Case Refactoring<br />city = case superhero<br />when “superman”<br /> “metropolis”<br />when “batman”<br /> “gotham_city”<br />else <br /> “central_city”<br />end<br />
    17. 17. Iterators<br />
    18. 18. Iterators: Conditional Looping<br />“while” allows us to loop through code while a set condition is true<br />x = 1<br />while x &lt; 10<br /> puts x.to_s + “ iteration”<br />x += 1<br />end<br />
    19. 19. Creating a new array<br />x = [1, 2, 3, 4]<br />=&gt; [1, 2, 3, 4]<br />x = %w(1 2 3 4) <br />=&gt; [“1”, “2”, “3”, “4”]<br />chef =, “bork”)<br />=&gt; [“bork”, “bork”, bork”]<br />
    20. 20. Accessing Array Values<br />a = [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot; ] <br />a[0] #=&gt; &quot;a”<br />a[2] #=&gt; &quot;c”<br />a[6] #=&gt; nil<br />
    21. 21. Creating a Hash<br />h = { &quot;a&quot; =&gt; 100, &quot;b&quot; =&gt; 200 }<br />h[“a”]<br />h = { 1 =&gt; “a”, “b” =&gt; “hello” }<br />h[1]<br />
    22. 22. Operations on Hashes: Merge<br />h1 = { &quot;a&quot; =&gt; 100, &quot;b&quot; =&gt; 200 } <br />=&gt; {&quot;a&quot;=&gt;100, &quot;b&quot;=&gt;200}<br />h2 = { &quot;b&quot; =&gt; 254, &quot;c&quot; =&gt; 300 }<br />=&gt;{&quot;b&quot;=&gt;254, &quot;c&quot;=&gt;300}<br />h3 = h1.merge(h2)<br />=&gt; {&quot;a&quot;=&gt;100, &quot;b&quot;=&gt;254, &quot;c&quot;=&gt;300}<br />h1<br />=&gt; {&quot;a&quot;=&gt;100, &quot;b&quot;=&gt;200}<br />h1.merge!(h2)<br />=&gt; {&quot;a&quot;=&gt;100, &quot;b&quot;=&gt;254, &quot;c&quot;=&gt;300}<br />
    23. 23. Operations on Hashes<br />h = { &quot;a&quot; =&gt; 100, &quot;b&quot; =&gt; 200 } <br />h.delete(&quot;a”)<br />h = { &quot;a&quot; =&gt; 100, &quot;b&quot; =&gt; 200, &quot;c&quot; =&gt; 300, &quot;d&quot; =&gt; 400 }<br />letters = h.keys <br />h = { &quot;a&quot; =&gt; 100, &quot;b&quot; =&gt; 200, &quot;c&quot; =&gt; 300 } <br />numbers = h.values<br />
    24. 24. Times<br />5.times{ puts “hello” }<br />99.times do |beer_num| <br /> puts &quot;#{beer_num} bottles of beer”<br />end<br />
    25. 25. Each<br />superheroes = [“catwoman”, “batman”, “wonderwoman”]<br />superheroes.each do | s | <br />puts “#{ s } save me!” <br />end<br />wonderwoman save me!<br />batman save me!<br />catwoman save me!<br />
    26. 26. Blocks<br />
    27. 27. Blocks<br />def dos_veces<br /> yield<br /> yield<br />end<br />dos_veces { puts &quot;Hola” }<br />Hola<br />Hola<br />Yield executes the block<br />This is a Block!<br />{<br />
    28. 28. Yield with Parameters<br />def bands<br />yield(“abba”, “who”)<br />end<br />bands do |x,y| puts x,y <br />end<br />abba<br />who<br />Yield sends its parameters as arguments to the block<br />yield(“abba”, ”who”) sends “abba” and “who” to |x, y|<br />x is set to “abba”<br />y is set to “who”<br />
    29. 29. Mr. Neighborly&apos;s Humble Little Ruby BookJeremy McAnally<br />The Well-Grounded RubyistCovering Ruby 1.9David A. Black<br />Ruby Koansa walk along the path to enlightenment<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.