SlideShare a Scribd company logo
Ruby Programming Language
puts “Hello Ruby!”
Clean, Elegant
      and
Meaningful Syntax
test_string = 'string for test'
puts 'matched' if test_string.match 'string'




files = Dir['*.txt']
for file in files
    file_ref = open file
    file_ref.each_line { |line| puts line.reverse.upcase }
end
Everything is an Object


10.times { puts “Hello World!” }   “Hello World!”.methods
Dynamic Typing,
  Duck Typing
     and
 Open Classes
a_variable = 'a b c'.split(' ')   #=> ['a','b','c']
a_variable = a_variable.join(' ') #=> 'a b c'



def a_function object_par
     puts object_par.crazy_method
end

a_function [1,2,3] #=> error: undefined method 'crazy_method' for class Array

class Array
    def crazy_method
         return 'crazy method for an array'
    end
end

a_function [1,2,3] #=> puts 'crazy method for an array'
a_variable = 'a b c'.split(' ')   #=> ['a','b','c']
a_variable = a_variable.join(' ') #=> 'a b c'



def a_function object_par
     puts object_par.crazy_method
end

a_function [1,2,3] #=> error: undefined method 'crazy_method' for class Array

class Array
    def crazy_method
         return 'crazy method for an array'
    end
end

a_function [1,2,3] #=> puts 'crazy method for an array'
def a_function object_par
     puts object_par.crazy_method
end

some_obj = SomeClass.new

a_function some_obj #=> error: undefined method 'crazy_method' for class SomeClass

def some_obj.crazy_method
     return 'this is a crazy feature'
end

a_function some_obj #=> puts 'this is a crazy_feature'
def a_function object_par
     puts object_par.crazy_method
end

some_obj = SomeClass.new

a_function some_obj #=> error: undefined method 'crazy_method' for class SomeClass

def some_obj.crazy_method
     return 'this is a crazy feature'
end

a_function some_obj #=> puts 'this is a crazy_feature'
"if it walks like a duck and quacks like a
           duck, then it is a duck”
Blocks
def square an_array
    return an_array.map { |e| e*e }
end




lines_of_a_doc.each_with_index do |line,i|
    if i.even? then
          puts 'even line: #{line}'
    else
          puts 'odd line: ' + line
    end
end
[1,2,3,4].select { |e| e.even? }
#=> [2,4]

[1,2,3,4].collect { |e| e.even? }
#=> [true,false,true,false]

[1,2,3,4].inject(0) { |sum,e| sum += e }
#=> 10
def my_for n
   n.times { |n| yield(n) }
end

my_for 3 { |i| puts i }

#=> 1
#=> 2
#=> 3
Mix-in
class Books < Collection
    def initialize
         @books = SomeReader.new('some_file_with_books').get_books
    end

      include Enumerable

      def each
          @books.each { |book| yield(book) }
      end
end




        Class Books now have
 map, select, inject, grep, find_all, include?
                 and more
Testing
require 'test/unit'

class TestHtmlParser < Test::Unit::TestCase

      must “find all imgs” do
          parser = HtmlParser.new '<div class='a'> <br/> <img src='img1.jpg'>n
                                   <p><img src='img2.jpg'></body>'
          assert_equal parser.parse_imgs, ['img1.jpg','img2.jpg']
      end
end

class HtmlParser

      def initialize html_doc
           @content = html_doc
      end

      def parse_imgs
          @content.scan(/imgs+src='(.+?)'/)
      end
end
Standard Library

● erb - .rhtml
● sockets

● threads

● html/xml parser

● ftp, http, imap, pop3, smtp

● tk

● pstore
And Gems!

$ gem install rails

$ gem install rspec

$ gem search twitter

 13,007 gems and counting!
Productivity and fun

    And that's it!



       Duda Dornelles
 dudassdornelles@gmail.com

More Related Content

What's hot (19)

PPTX
Ruby Metaprogramming
Thaichor Seng
 
KEY
PHPSpec BDD for PHP
Marcello Duarte
 
PPT
Rails 2010 Workshop
dtsadok
 
ODP
Decorators in Python
Ben James
 
PDF
Benchmarking Perl (Chicago UniForum 2006)
brian d foy
 
PPTX
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
 
KEY
(Parameterized) Roles
sartak
 
ODP
Moose: Perl Objects
Lambert Lum
 
PDF
Metaprogramming and Folly
Haseeb Qureshi
 
PDF
Python decorators
Guillermo Blasco Jiménez
 
PDF
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Puppet
 
PDF
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet
 
PDF
Designing with Groovy Traits - Gr8Conf India
Naresha K
 
PPTX
Python decorators
Alex Su
 
PPT
Constructive Destructor Use
metaperl
 
PDF
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
PDF
Perl.Hacks.On.Vim
Lin Yo-An
 
PDF
RubyEnRails2007 - Dr Nic Williams - Keynote
Dr Nic Williams
 
PDF
Introduction to CoffeeScript
Stalin Thangaraj
 
Ruby Metaprogramming
Thaichor Seng
 
PHPSpec BDD for PHP
Marcello Duarte
 
Rails 2010 Workshop
dtsadok
 
Decorators in Python
Ben James
 
Benchmarking Perl (Chicago UniForum 2006)
brian d foy
 
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
 
(Parameterized) Roles
sartak
 
Moose: Perl Objects
Lambert Lum
 
Metaprogramming and Folly
Haseeb Qureshi
 
Python decorators
Guillermo Blasco Jiménez
 
Replacing "exec" with a type and provider: Return manifests to a declarative ...
Puppet
 
Puppet Camp Chicago 2014: Smoothing Troubles With Custom Types and Providers ...
Puppet
 
Designing with Groovy Traits - Gr8Conf India
Naresha K
 
Python decorators
Alex Su
 
Constructive Destructor Use
metaperl
 
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Kacper Gunia
 
Perl.Hacks.On.Vim
Lin Yo-An
 
RubyEnRails2007 - Dr Nic Williams - Keynote
Dr Nic Williams
 
Introduction to CoffeeScript
Stalin Thangaraj
 

Viewers also liked (20)

PPT
Wiki
donz23
 
PAGES
3.30.2011
claire9831
 
DOCX
İStanbulda GüNeş Konferansı
hakki surel
 
PPTX
How to Use HealthyCity.org for Grant Writing & Reporting
Healthy City
 
PDF
“Living A Spirit Filled Life"
Don McClain
 
PPT
Corporate presentatie HRD adviesbureau GITP
GITP
 
PPTX
Whats Auth Got To Do With It
Bryan Tuttle
 
PPT
University of Manchester PGT Administrator ETD Briefing Session
Philip Butler
 
PPT
Henry Hobson Richardson
gueste1c16e
 
PPT
我的照片
wsb54321
 
PPT
直角座標
guest9d27661
 
PDF
When I Have A Convenient Season
Don McClain
 
PDF
Tinas Slide Show
mamafeenix
 
PDF
03 wrath of_god_against_jews
Don McClain
 
PDF
Zealous for good
Don McClain
 
PDF
FAREEDA moodboard
Urdun Mubdi3
 
PDF
It Is Well With My Soul
Don McClain
 
PPTX
мнения ученых о воде
NeKsE
 
PPT
Henry Hobson Richardson
gueste1c16e
 
PPT
Henry Hobson Richardson
gueste1c16e
 
Wiki
donz23
 
3.30.2011
claire9831
 
İStanbulda GüNeş Konferansı
hakki surel
 
How to Use HealthyCity.org for Grant Writing & Reporting
Healthy City
 
“Living A Spirit Filled Life"
Don McClain
 
Corporate presentatie HRD adviesbureau GITP
GITP
 
Whats Auth Got To Do With It
Bryan Tuttle
 
University of Manchester PGT Administrator ETD Briefing Session
Philip Butler
 
Henry Hobson Richardson
gueste1c16e
 
我的照片
wsb54321
 
直角座標
guest9d27661
 
When I Have A Convenient Season
Don McClain
 
Tinas Slide Show
mamafeenix
 
03 wrath of_god_against_jews
Don McClain
 
Zealous for good
Don McClain
 
FAREEDA moodboard
Urdun Mubdi3
 
It Is Well With My Soul
Don McClain
 
мнения ученых о воде
NeKsE
 
Henry Hobson Richardson
gueste1c16e
 
Henry Hobson Richardson
gueste1c16e
 
Ad

Similar to Ruby Programming Language (20)

KEY
An introduction to Ruby
Wes Oldenbeuving
 
PDF
Ruby is Awesome
Astrails
 
KEY
Refactor like a boss
gsterndale
 
KEY
Ruby
Kerry Buckley
 
PDF
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
PDF
Hidden Gems of Ruby 1.9
Aaron Patterson
 
KEY
Module Magic
James Gray
 
PDF
Ruby on Rails
bryanbibat
 
PDF
Designing Ruby APIs
Wen-Tien Chang
 
PDF
Ruby 2.0
Uģis Ozols
 
KEY
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
PDF
Ruby and rails - Advanced Training (Cybage)
Gautam Rege
 
PDF
Ruby 入門 第一次就上手
Wen-Tien Chang
 
PPTX
Ruby :: Training 1
Pavel Tyk
 
KEY
A tour on ruby and friends
旻琦 潘
 
PDF
Ruby and Rails by Example (GeekCamp edition)
bryanbibat
 
PDF
Let’s Talk About Ruby
Ian Bishop
 
PDF
Ruby 程式語言入門導覽
Wen-Tien Chang
 
PDF
Active Support Core Extensions (1)
RORLAB
 
PPTX
Ruby for .NET developers
Max Titov
 
An introduction to Ruby
Wes Oldenbeuving
 
Ruby is Awesome
Astrails
 
Refactor like a boss
gsterndale
 
Slides chapter3part1 ruby-forjavaprogrammers
Giovanni924
 
Hidden Gems of Ruby 1.9
Aaron Patterson
 
Module Magic
James Gray
 
Ruby on Rails
bryanbibat
 
Designing Ruby APIs
Wen-Tien Chang
 
Ruby 2.0
Uģis Ozols
 
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
Ruby and rails - Advanced Training (Cybage)
Gautam Rege
 
Ruby 入門 第一次就上手
Wen-Tien Chang
 
Ruby :: Training 1
Pavel Tyk
 
A tour on ruby and friends
旻琦 潘
 
Ruby and Rails by Example (GeekCamp edition)
bryanbibat
 
Let’s Talk About Ruby
Ian Bishop
 
Ruby 程式語言入門導覽
Wen-Tien Chang
 
Active Support Core Extensions (1)
RORLAB
 
Ruby for .NET developers
Max Titov
 
Ad

Ruby Programming Language

  • 3. Clean, Elegant and Meaningful Syntax
  • 4. test_string = 'string for test' puts 'matched' if test_string.match 'string' files = Dir['*.txt'] for file in files file_ref = open file file_ref.each_line { |line| puts line.reverse.upcase } end
  • 5. Everything is an Object 10.times { puts “Hello World!” } “Hello World!”.methods
  • 6. Dynamic Typing, Duck Typing and Open Classes
  • 7. a_variable = 'a b c'.split(' ') #=> ['a','b','c'] a_variable = a_variable.join(' ') #=> 'a b c' def a_function object_par puts object_par.crazy_method end a_function [1,2,3] #=> error: undefined method 'crazy_method' for class Array class Array def crazy_method return 'crazy method for an array' end end a_function [1,2,3] #=> puts 'crazy method for an array'
  • 8. a_variable = 'a b c'.split(' ') #=> ['a','b','c'] a_variable = a_variable.join(' ') #=> 'a b c' def a_function object_par puts object_par.crazy_method end a_function [1,2,3] #=> error: undefined method 'crazy_method' for class Array class Array def crazy_method return 'crazy method for an array' end end a_function [1,2,3] #=> puts 'crazy method for an array'
  • 9. def a_function object_par puts object_par.crazy_method end some_obj = SomeClass.new a_function some_obj #=> error: undefined method 'crazy_method' for class SomeClass def some_obj.crazy_method return 'this is a crazy feature' end a_function some_obj #=> puts 'this is a crazy_feature'
  • 10. def a_function object_par puts object_par.crazy_method end some_obj = SomeClass.new a_function some_obj #=> error: undefined method 'crazy_method' for class SomeClass def some_obj.crazy_method return 'this is a crazy feature' end a_function some_obj #=> puts 'this is a crazy_feature'
  • 11. "if it walks like a duck and quacks like a duck, then it is a duck”
  • 13. def square an_array return an_array.map { |e| e*e } end lines_of_a_doc.each_with_index do |line,i| if i.even? then puts 'even line: #{line}' else puts 'odd line: ' + line end end
  • 14. [1,2,3,4].select { |e| e.even? } #=> [2,4] [1,2,3,4].collect { |e| e.even? } #=> [true,false,true,false] [1,2,3,4].inject(0) { |sum,e| sum += e } #=> 10
  • 15. def my_for n n.times { |n| yield(n) } end my_for 3 { |i| puts i } #=> 1 #=> 2 #=> 3
  • 17. class Books < Collection def initialize @books = SomeReader.new('some_file_with_books').get_books end include Enumerable def each @books.each { |book| yield(book) } end end Class Books now have map, select, inject, grep, find_all, include? and more
  • 19. require 'test/unit' class TestHtmlParser < Test::Unit::TestCase must “find all imgs” do parser = HtmlParser.new '<div class='a'> <br/> <img src='img1.jpg'>n <p><img src='img2.jpg'></body>' assert_equal parser.parse_imgs, ['img1.jpg','img2.jpg'] end end class HtmlParser def initialize html_doc @content = html_doc end def parse_imgs @content.scan(/imgs+src='(.+?)'/) end end
  • 20. Standard Library ● erb - .rhtml ● sockets ● threads ● html/xml parser ● ftp, http, imap, pop3, smtp ● tk ● pstore
  • 21. And Gems! $ gem install rails $ gem install rspec $ gem search twitter 13,007 gems and counting!
  • 22. Productivity and fun And that's it! Duda Dornelles [email protected]