Rochester on Rails: Introduction to Ruby

2,725 views

Published on

A brief and basic introduction to the syntax of Ruby 1.8, along with a few nice language features. This is a talk I gave at one of our first Rochester on Rails meetings.

Published in: Business, Technology
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,725
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
70
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Rochester on Rails: Introduction to Ruby

  1. 1. Ruby Programmers’ Best Friend Jason Morrison January 19, 2006 Rochester on Rails
  2. 2. History
  3. 3. Ruby Yukihiro “Matz” Matsumoto February 24, 1993
  4. 4. Perl Java Python Ruby PHP 1987 1991 1993 1995
  5. 5. Examples!
  6. 6. 5.times { print “Ruby! " }
  7. 7. Ruby! Ruby! Ruby! Ruby! Ruby! 5.times { print “Ruby! " }
  8. 8. [‘one’,‘two’,‘three’].each {|num| puts num.capitalize }
  9. 9. [‘one’,‘two’,‘three’].each {|num| puts num.capitalize } One Two Three
  10. 10. Everything is an object
  11. 11. -21474836480.abs
  12. 12. -21474836480.abs 21474836480
  13. 13. “ Rats live on no evil star”  .reverse.capitalize
  14. 14. “ Rats live on no evil star”  .reverse.capitalize “ Rats live on no evil star”
  15. 15. 3.hours.from_now
  16. 16. 3.hours.from_now Thu Jan 19 22:05:00 Eastern  Standard Time 2006
  17. 17. Conventions
  18. 18. Variables colored_index_cards
  19. 19. Class Names DromedaryDiner
  20. 20. Symbols :creme_de_menthe
  21. 21. Instance Variables @euros_per_liter
  22. 22. Constants Kilograms_Per_Pound
  23. 23. Syntax
  24. 24. Methods def say_hello(name) result = “Hello, #{name}!” return result end puts say_hello(“world”)
  25. 25. Methods def say_hello(name) “Hello, #{name}!” end puts say_hello(“world”)
  26. 26. Classes class MathWhiz def say_square(value) puts value * value end end sam = MathWhiz.new sam.say_square(5)
  27. 27. Open Classes class Integer def squared self * self end end 5.squared #=> 25
  28. 28. Inheritance class Whopper < Burger @maker = “Burger King” @calories = 0.67 * 10**3 end
  29. 29. Class Methods class FileUtil def self.mkdir(dir) # do it! end end FileUtil.mkdir(“oranges”)
  30. 30. Modules module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. end end
  31. 31. Modules require &quot;trig&quot; y = Trig.sin(Trig::PI/4) 0.707106780551956
  32. 32. Mixins module Debug def whoAmI? “ #{self.type.name} ” + “ (##{self.id}): ” + “ #{self.to_s}&quot; end end
  33. 33. Mixins class Phonograph include Debug # ... end ph = Phonograph.new(&quot;West End Blues&quot;) ph.whoAmI? &quot;Phonograph (#537766170): West End Blues&quot;
  34. 34. Attributes class PlainOldRubyObject attr_accessor :food, :drinks attr_reader :advice attr_writer :write_only end
  35. 35. Scope class Poet #public by default def poetry end protected def family_legacy end private def hopes_and_dreams end end
  36. 36. Arrays
  37. 37. foo = [] foo << 1 #=> [1] foo << 2 #=> [1, 2] foo << 3 #=> [1, 2, 3]
  38. 38. bar = [1, 2, 3] bar << 4 #=> [1, 2, 3, 4] bar << 5 #=> [1, 2, 3, 4, 5] bar << 6 #=> [1, 2, 3, 4, 5, 6]
  39. 39. folks = %w( Charles Ed Amanda ) #=> [“Charles”, “Ed”, “Amanda”] folks[1] Ed
  40. 40. Hashes
  41. 41. menu = { :douglas_sirk_steak => 17.50, :vanilla_coke => 2.75, :durwood_kirby_burger => 9.75, :five_dollar_shake => 5.00 } menu[:vanilla_coke] 2.75
  42. 42. Flow
  43. 43. if ( score >= 5000 ) puts “You win!” elsif ( score <= 0 ) puts “Game over.” else puts “Current score: #{score}” end
  44. 44. puts “Watch out!” if lion_distance < 5
  45. 45. Blocks
  46. 46. 1.upto(5) { |x| puts x } 1 2 3 4 5
  47. 47. 5.downto(1) do |time| print “#{time}... ” puts “!” if time <= 3 end 5... 4... 3... ! 2... ! 1... !
  48. 48. Fín!
  49. 49. Homepage www.ruby-lang.org Try Ruby in your browser tryruby.hobix.com Ruby with humor  www.poignantguide.net

×