Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Intro To Ruby

941 views

Published on

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

Published in: Technology
  • Be the first to comment

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 />http://en.wikipedia.org/wiki/Ruby_(programming_language)#History<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 />thing.do(4)<br />thing.do 4<br />thing.send “do”, 4<br />
  8. 8. Operators are Methods<br />thing.do 4<br />thing.do(4)<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 = Array.new(3, “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 enlightenmenthttp://github.com/edgecase/ruby_koans<br />

×