!SLIDE
Jux.com
Slides at: gist.github.com/3944262
!SLIDE
- 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
- Interpreted, object-oriented, dynamically/duck-typed
- Balances "functional programming with imperative programming"
- Flexible syntax
- [Almost] everything is an expression^
!SLIDE
- "Statements do not return results and are executed solely for their side effects, while expressions always return a result"
- "Expressions have a value, statements do not"
!SLIDE
@@@ 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
Lisp-y, without the parens.
@@@ ruby
search_engines =
%w[Google Yahoo MSN].map do |engine|
"http://www." + engine.downcase + ".com"
end
!SLIDE
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')
!SLIDE
- Instance variables
@foo
- Getters/setters
f.surname
f.surname = 'Smith'
- String interpolation
"#{first} #{last}"
!SLIDE
- 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
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
@@@ 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
Aidan Feldman