Skip to content

Instantly share code, notes, and snippets.

@afeld
Last active September 13, 2022 18:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save afeld/3944262 to your computer and use it in GitHub Desktop.
Save afeld/3944262 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Months: Ruby presentation
git clone git://gist.github.com/3944262.git seven_langs_ruby
cd seven_langs_ruby
gem install gli -v 1.6.0
gem install showoff
showoff serve
open "http://localhost:9090"

!SLIDE

Aidan Feldman

Jux.com

Slides at: gist.github.com/3944262

!SLIDE

Background

  • Created in 1993 in Japan by Yukihiro Matsumoto ("Matz")
  • Inspired by Lisp, Smalltalk & Perl
  • Emphasis on programmer productivity
  • Gained in popularity w/ Rails ca. 2006

!SLIDE

Overview

!SLIDE

Expression vs. Statement

!SLIDE

Everything is an Object

@@@ ruby
6.is_a? Object
nil.is_a? Object

a = Object.new
a.is_a? Object

Class.is_a? Object
Object.is_a? Object

!SLIDE

Blocks

Lisp-y, without the parens.

@@@ ruby
search_engines =
  %w[Google Yahoo MSN].map do |engine|
    "http://www." + engine.downcase + ".com"
  end

!SLIDE

Mixins

Alternative to multiple inheritance, e.g. Enumerable.

@@@ ruby
class Family
  include Enumerable

  attr_accessor :surname
  attr_accessor :father, :mother, :child

  def each
    yield "#{@father} #{@surname}"
    yield "#{@mother} #{@surname}"
    yield "#{@child} #{@surname}"
  end
end

f = Family.new
f.surname = 'Smith'
f.father = 'Bob'
f.mother = 'Alice'
f.child = 'Carol'

f.each do|person|
  puts person
end

puts f.include?('Bob Smith')

c/o Michael Morin

!SLIDE

Things to Notice

  • Instance variables
    • @foo
  • Getters/setters
    • f.surname
    • f.surname = 'Smith'
  • String interpolation
    • "#{first} #{last}"

!SLIDE

Syntactic Sugar

  • Operations (including arithmetic) are simply methods
  • Parentheses often optional
  • Methods and properties are somewhat interchangeable.
  • Operators in english, e.g. unless, and, or

ex.

@@@ ruby
'foo bar'.split.size.to_s + ' words'

!SLIDE

Monkey-Patching

Add functionality to previously-defined classes.

@@@ ruby
class Fixnum
  def k
    self * 1000
  end
end

puts 10
puts 10.k

…or override.

@@@ ruby
class Fixnum
  def +(x)
    self.-(x)
  end
end

puts 3 + 2

!SLIDE

MetaProgramming

@@@ ruby
Kernel.const_get('Array').new

6.methods
6.respond_to? :to_s
6.send :to_s
6.method(:to_s).call


class Family
  attr_accessor :surname
  attr_accessor :father, :mother

  def method_missing(meth)
    if meth.to_s =~ /^(\w+)_full$/
      "#{self.send($1)} #{self.surname}"
    else
      super
    end
  end
end

f = Family.new
f.surname = 'Smith'
f.father = 'Bob'
f.mother = 'Alice'

f.father_full
f.mother_full
f.son_full

!SLIDE

Fin.

Aidan Feldman

@aidanfeldman

afeld.me

#preso,
.slide {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: auto;
height: auto;
overflow: auto;
}
pre.sh_sourceCode {
font-size: 2.5em;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment