Introducing Ruby
Upcoming SlideShare
Loading in...5

Like this? Share it with your network


Introducing Ruby



Basic introduction to the history and uses of the Ruby programming lanaguage.

Basic introduction to the history and uses of the Ruby programming lanaguage.



Total Views
Views on SlideShare
Embed Views



1 Embed 3 3



Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • * A dynamic object-oriented programming language <br /> * Inspired by Perl, Eiffel, Ada, Lisp and Smalltalk. <br /> * Its creator stated his goal was to "make Ruby natural, not simple." <br /> <br /> Ruby is a very powerful language that is also very flexible. It is pervasively <br /> object-oriented. It is a general purpose language, so it is useful in the same <br /> places that Perl, PHP, etc. would be used.
  • Developed by Yukihiro "matz" Matsumoto in 1993 with its first public <br /> release in Japan in 1995. <br /> <br /> It saw its first English announcement in 1998 and <br /> was followed by the publication of the Programming Ruby book in 2000 by Dave <br /> Thomas and Andy Hunt. <br /> <br /> The second edition of Programming Ruby, known as the <br /> "Pickaxe Book" was released in 2004.
  • The Ruby world is transitioning to 1.9 and most major and currently maintained <br /> projects have made the switch to supporting 1.9. The community is tracking the <br /> progress of this transition with relation to Gems as <br /> <br /> Beyond the "official" interpreter, usually referred to as "MRI," there are a <br /> number of projects to develop alternative interpreters for various reasons and <br /> to met various needs. <br /> <br /> * Enterprise: Ruby 1.8 with better memory handling and other performance optimizations <br /> * JRuby: Ruby on top of the JVM, fastest 1.8 implementation around. <br /> * MacRuby: Ruby on top of Apple&apos;s Objective-C runtime. <br /> * IronRuby: Ruby on top of the .NET DLR. <br /> * Rubinius: Ruby developed with a tight C++ main interpreter and most of the rest of the language developed the Ruby subset understood by the main interpreter.
  • If you are on Windows you should use Cygwin. Or, better yet, move to a unix <br /> environment since many of the best and most useful Ruby Gems use C extensions <br /> which are a pain to get working outside of a unix environment. There is the <br /> one-click installer as well, but it is badly outdated, and the straight binary <br /> releases from <br /> <br /> For Linux you can use your favorite package manager to install ruby, irb and <br /> rdoc to get the whole base system. Some distros are starting to make 1.9 <br /> available but it usually has a suffix on all the commands which make them <br /> more cumbersome. <br /> <br /> For OS X if you have Leopard you already have Ruby available but if you use <br /> MacPorts you can get the latest version and even 1.9 with no suffix. Mac users <br /> also have access to the MacRuby project which is very promising and should <br /> eventually make OS X an even better platform for Rubyists. <br /> <br /> If you don&apos;t want to install Ruby you can play with an online interactive <br /> environment at
  • Ruby&apos;s command set provides a rich set of features for exploring the language, <br /> its documentation and different ways of using it.
  • Ruby has a number of conventions which are collectively referred to as the <br /> "Ruby Way." <br /> <br /> You are free to ignore these conventions if you like, but don&apos;t expect much <br /> love from your fellow Rubyists. And you can expect a nasty warning from the <br /> interpreter if you fiddle with what it expects of a constant, although you are <br /> actually free to change a constant. <br /> <br /> Module and Class names are actually constants which is why some consider it acceptable to use camel case for constants as well.
  • One big thing to understand from the start with Ruby is that everything is an <br /> object. <br /> <br /> With the exception of reserved keywords everything is an object in Ruby. This <br /> allows for really interesting programming techniques in terms of chaining <br /> method invocation and even manipulation of parameters. <br /> <br /> Object is the parent of all classes. <br /> <br /> Duck typing is the pattern Ruby follows where behavior is closely coupled to type.
  • Another big thing in Ruby are symbols. Because of Ruby&apos;s object model two <br /> strings which have the same character sequences are not the same. They have <br /> different object ids and occupy separate places in memory. Some have described <br /> symbols as immutable strings but this is really an oversimplification. <br /> <br /> Used with hashes for keys and as pseudo-constants. <br /> <br /> There is a lot more to symbols and whole blogs have focused on uses for <br /> symbols so go Google Ruby symbols if you want to learn more.
  • If you are working with a class you have two special kinds of variables <br /> available to you. Instance variables are unique to a particular instance of an <br /> object while class variables are unique to all instances of a given class.
  • Strings are really straight forward. Ruby supports single and double quoted <br /> strings, as well as HEREDOC style strings and alternative encapsulated <br /> strings. Strings also support interpolation of variables as well. <br /> <br /> Escaped new lines are optional when using certain ruby methods. Most of the <br /> time you don&apos;t need to be explicit with new lines, Ruby uses them where you <br /> would expect automatically.
  • Ruby deals well with integers and floating point values as you would expect. <br /> It supports decimal and scientific notation as well as hex and octal.
  • Something that is missing that some may wonder about is an explicit return. In <br /> Ruby the last expression within a method is returned as the result of the <br /> method by default. So explicit returns are not needed. You can do an explicit <br /> return however.
  • A thing to note about Ruby classes is that they only support single <br /> inheritance, although there is an inheritance hierarchy at work in Ruby. <br /> Inheritance is also pretty straightforward to achieve.
  • The way you call the constructor is by using the new method. To define a <br /> constructor for a class just define a method called initialize.
  • You can also define attributes for classes. These are just set/get accessors <br /> for the class, if defined they are usually tied to an instance variable.
  • You can invoke any inherited methods by calling super().
  • Modules are collections of methods and constants. They are used to achieve <br /> much the same ends as multiple inheritance would provide but without the <br /> confusion.
  • Modules get mixed in to classes using the include method.
  • Something you should have noticed if you were following along with the code <br /> examples in irb is that I didn&apos;t redefine the entire class every time I did <br /> something new. One of the most useful and potentially troublesome features of <br /> Ruby is the open nature of classes and modules. You can just open them up and <br /> add to them. Heck, there are even ways to remove methods and the like. Here&apos;s <br /> an example of a modification to the way numbers work in Ruby. <br /> <br /> You can add and redefine methods on any class or module very easily in Ruby. <br /> Again, this is a very powerful but also dangerous capability.
  • Ruby has all the typical control structures and some of its own interesting ones. <br /> <br /> That last one is a fun way to work with collections like arrays or hashes.
  • Ruby has all the types you would expect from a good dynamic language. Most <br /> we&apos;ve already discussed but here&apos;s a quick rundown of the standard ones, <br /> including some we didn&apos;t talk about.
  • Ruby supports closures in the forms of blocks, procs and lambdas. Blocks are <br /> basically pieces of code that you can pass as arguments to method calls. We <br /> saw a basic block with control structures. <br /> <br /> You can think of blocks like anonymous methods that are able to capture the <br /> context they are called in. Procs are essentially blocks that have been stored <br /> in a variable. This allows a the Proc to be more reusable than a block and <br /> allows the developer to avoid repetition. <br /> <br /> Ruby also provides lambdas which are similar to Procs but check the number of <br /> variables passed to them. For more on these three special structures in Ruby <br /> you can Google them and learn all the different ways they can be used.
  • The best way to think of RubyGems is to think of it like CPAN for Ruby. It is <br /> the de facto standard for distributing reusable Ruby code. Lots of libraries, <br /> and most Ruby applications are distributed as Gems. <br /> <br /> You can get RubyGems either by default with Ruby 1.9 or downloaded from <br /> But lets look at how you use RubyGems a little bit. Many Linux distros also have it available as a package.
  • When compared to the package repositories of other popular languages Ruby is <br /> definitely a productive community. The numbers for Github may give a slightly <br /> exaggerated impression since many of the gems hosted with them are duplicates <br /> of gems distributed through Rubyforge or are unstable development packages. <br /> <br /> However, it is clear that Ruby is a healthy community of users sharing their <br /> work with one another.

Introducing Ruby Presentation Transcript

  • 2. WHO IS THIS GUY? James Thompson @plainprogrammer Web Administrator & Student @ New Orleans Baptist Theological Seminary
  • 3. WHAT IS RUBY? Ruby is a dynamic, open source programming language with a focus on simplicity and productivity –
  • 4. HISTORY Created by Yukihiro “matz” Matsumoto Started development in 1993 First public release in 1995 (Japan) First English release in 1998 “make Ruby natural, not simple.”
  • 5. CURRENT STATE Legacy Stable: 1.8.7p72 Current Stable: 1.9.1p129 A community in transition Alternatives to MRI
  • 6. INSTALLING RUBY •Windows: Cygwin is the best option. One-click installer available but out of date. •Linux: apt-get install ruby rdoc irb •OS X: Already installed in 10.5. Available through MacPorts as well.
  • 8. RUBY’S COMMANDS •ruby main interpreter •irb interactive ruby shell •ri ruby information, manpages for classes •rdoc ruby documentation compiler •erb ruby templating system
  • 9. RUBY CONVENTIONS •variable_name •method_name •ClassName •ModuleName •ConstantName | CONTSTANT_NAME
  • 11. OBJECT ORIENTATION 5.class #=> Fixnum In ruby everything is an object. 3.9.class #=> Float "Hello".class #=> String Fixnum.class #=> Class This is both powerful and nil.class #=> NilClass different.
  • 12. SYMBOLS :a_symbol opts = { :a_key => "Some value!", :another_key => "another value..." }
  • 13. INSTANCE & CLASS VARIABLES @instance_variable @@class_variable
  • 14. STRINGS name = 'BarCamp Joe' greeting = "Hello, #{name}!" long_greeting <<-EOS Hello my old friend, #{name}! How have you been? EOS alternate_greeting = %{G'day, #{name.upcase}!}
  • 15. NUMBERS a = 1 b = 2.0 c = 2.3e5 d = 0x3F e = 0244
  • 16. METHODS def say_hello(name) result = "Hello, #{name}!" end puts say_hello("BarCamp NOLA")
  • 17. METHODS def say_hello(name) result = "Hello, #{name}" return result end puts say_hello("BarCamp NOLA")
  • 18. CLASSES class Greeter def say_hello(name) puts "Hello, #{name}!" end end doorman = doorman.say_hello("BarCampers")
  • 19. CLASSES class RudeGreeter < Greeter def say_hello(name) puts "" end end
  • 20. CLASSES class Greeter def initialize(name) @greeters_name = name end end
  • 21. CLASSES class Greeter attr_reader :name end
  • 22. CLASSES class RudeGreeter < Greeter def initialize(name, rudeness) @rudeness = rudeness super(name) end end
  • 23. MODULES module Farewell def bid_farewell(name) "Good bye, #{name}!" end end
  • 24. MODULES class Greeter include Farewell end doorman ="John") doorman.bid_farewell("BarCampers")
  • 25. CLASS/MODULE MUTABILITY class Numeric def plus(n) self.+(n) end end y = 6 #=> 11
  • 26. CONTROL STRUCTURES if "a" == "a" puts "a" end unless "a".nil? puts "a" end puts "a" unless "a".nil? name = case when num == 1: "one" when num == 2: "two" else num.to_s end [1,2,3].each do |value| puts value.to_s end
  • 27. TYPES •Numbers •Strings •Regular Expressions (/^(foo|bar)+$/) •Arrays ([1,2,3]) •Hashes •Ranges (1..10, "a"..."z") •Symbols
  • 28. BLOCKS, PROCS & LAMBDAS [1,2,3].each do |value| puts value.to_s end square = do |n| n ** 2 end
  • 29. RUBYGEMS
  • 30. CPAN Pear PECL RAA Rubyforge Github PyPI 17,000 12,750 8,500 4,250 Obsolete 0 Perl Ruby Python PHP PERL, PHP & RUBY PACKAGES
  • 31. SEEING WHAT’S OUT THERE gem list --remote gem list
  • 32. GETTING HELP gem help gem help commands
  • 33. INSTALLING GEMS gem install rails gem install mysql sqlite3-ruby gem install rails --no-ri --no-rdoc
  • 34. COMMANDS FOR GEMS rails --version
  • 35. REMOVING GEMS gem uninstall mysql
  • 36. WHY USE RUBY?
  • 37. QUESTIONS?