0
RUBY SEEN BY A C#
DEVELOPER
Emanuele DelBono - @emadb
I AM
I am a software
engineer and web
architect in
CodicePlastico. I write
web apps in C#,
javascript e ruby.
IT ISN’T A RUBY TUTORIAL
!

It’s a bunch of stuff that made me say
WOW!
C# (2002)
!

• Compiled, strongly typed
• OOP (multiparadigm)
• Delegate/lambda/enumerators
• ECMA standard
• Mono on Linu...
RUBY (1995)
• Dynamic
• Open Source
• Interpreted
• Multi inheritance through Mixin
• Fully OOP
HELLO WORLD
using System;!


public class Program

{!
public void Main()

{!
Console.WriteLine("Hello World");

}!
}!
HELLO WORLD

p 'hello world'
RAILS
class Employee < ActiveRecord::Base!
belongs_to :company!
attr_accessible :name, :surname, :role!
has_many :activiti...
ALL CODE IS EXECUTED
class MyClass < (rand < 0.5 ? Array : Hash)!
end!
WOW!
REAL OOP
class is an instance of class
class Cat
end
cat = Cat.new
puts cat
#<Cat:0x007fa2bc13d5d8>
puts cat.class
Cat
puts cat.class.class
Class
puts cat.class....
Klazz = Class.new

=> Klazz
puts Klazz

=> Klazz
puts Klazz.class
Class
k = Klazz.new
=> #<Klazz:0x007f8fc121ab58>
Foo = Class.new do
def bar
"I'm bar...an instance method of foo"
end
end
!

a = Foo.new
p a.bar
=> "hello"
WOW!
SINGLETON METHODS

We can add methods to a single instance
class Cat
def meow; ’meow’; end
end
cat = Cat.new
new_cat = Cat.new
def cat.argh
'argh'
end
p cat.meow
=> "meow"
p cat.arg...
?

EIGENCLASS
THE OBJECT MODEL
• Every object, classes included, has its
own “real class,” be it a regular class
or an eigenclass
• The ...
THE OBJECT MODEL
class C; end
class D < C; end
obj = D.new

#Object

C

#C

D
obj

Object

#D

#obj

Metaprogramming Ruby ...
WOW!
monkey patching
“a way to extend or modify the runtime
code of dynamic languages [...] without
altering the original sourc...
class String
def foo
to_s + ' fooed'
end
end
s = "hello"
puts s.foo
=> "hello fooed"
REMOVE METHODS
class String
remove_method :to_s
end
WOW!
duck typing
When I see a bird that walks like a duck
and swims like a duck and quacks like a
duck, I call that bird a duck...
class Type1
def foo; "I'm type1"; end
end
!

class Type2
def foo; "I'm type2"; end
end
def get_class
rand < 0.5 ? Type1 : ...
METHOD INTERCEPTION

Using alias and monkey patching we can
run through the execution
class
def
p
end
end
cat =

Cat
meow
"meow"

Cat.new

Cat.class_eval do
alias :meow_new :meow
def meow
p "i'm about to meow...
METAPROGRAMMING
Metaprogramming is the writing of
computer programs that write or
manipulate other programs (or
themselves...
METHOD MISSING
class Cat
def method_missing(method, *args)
#do something without failing
end
end
class Settings
def initialize(options)
options.each do |key, value|
self.instance_variable_set "@#{key}", value
self.class...
HOOKS
Since all code is executed. You can
intercept some “facts” about it.
inherited, append_features, included,
extend_ob...
WOW!
BUT THERE’S MORE
> [1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3]
> 1_000_000
> a = a || []
> a = [1,2,3]; a[-1]
> [1,2,3].s...
PHILOSOPHY
I hope to see Ruby help
every programmer in the
world to be productive, and
to enjoy programming, and
to be hap...
o = Object.new!
o.methods.count
=> 54

• ActiveRecord::Base => 367
• String => 161
• Fixnum => 128
SAME METRICS?
DIFFERENT WORLD?
RUBY PRO
• Simplicity
• REPL (irb, rails c, heroku run console)
• No ceremony
• One file app
• Community and frameworks
• ...
CONS
• Freedom bring responsibility
• Performance
• No tools for refactoring
• Tests are mandatory (mmh…)
DOES IT WORTH?
LINGUISTIC RELATIVITY
“The principle of linguistic relativity holds
that the structure of a language affects
the ways in w...
THE PRAGMATIC
PROGRAMMER
“Learn at least one new language every
year. Different languages solve the same
problems in diffe...
MORE LANGUAGES 

==

BETTER PROGRAMMER
Ruby seen by a C# developer
Ruby seen by a C# developer
Ruby seen by a C# developer
Ruby seen by a C# developer
Ruby seen by a C# developer
Ruby seen by a C# developer
Ruby seen by a C# developer
Ruby seen by a C# developer
Ruby seen by a C# developer
Upcoming SlideShare
Loading in...5
×

Ruby seen by a C# developer

3,272

Published on

What an expert and deep-rooted C# developer discover about Ruby. Here are the impressions, the oddities and the WOWs.

Published in: Technology, Education
0 Comments
19 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,272
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
26
Comments
0
Likes
19
Embeds 0
No embeds

No notes for slide

Transcript of "Ruby seen by a C# developer"

  1. 1. RUBY SEEN BY A C# DEVELOPER Emanuele DelBono - @emadb
  2. 2. I AM I am a software engineer and web architect in CodicePlastico. I write web apps in C#, javascript e ruby.
  3. 3. IT ISN’T A RUBY TUTORIAL ! It’s a bunch of stuff that made me say WOW!
  4. 4. C# (2002) ! • Compiled, strongly typed • OOP (multiparadigm) • Delegate/lambda/enumerators • ECMA standard • Mono on Linux and MacOS • “A better java”
  5. 5. RUBY (1995) • Dynamic • Open Source • Interpreted • Multi inheritance through Mixin • Fully OOP
  6. 6. HELLO WORLD using System;! 
 public class Program
 {! public void Main()
 {! Console.WriteLine("Hello World");
 }! }!
  7. 7. HELLO WORLD p 'hello world'
  8. 8. RAILS class Employee < ActiveRecord::Base! belongs_to :company! attr_accessible :name, :surname, :role! has_many :activities! before_save :update_role! scope :managers, -> { where(role: 'manager') }! ! def update_role! # ...! end! def foo! # ...! end ! end
  9. 9. ALL CODE IS EXECUTED class MyClass < (rand < 0.5 ? Array : Hash)! end!
  10. 10. WOW!
  11. 11. REAL OOP class is an instance of class
  12. 12. class Cat end cat = Cat.new puts cat #<Cat:0x007fa2bc13d5d8> puts cat.class Cat puts cat.class.class Class puts cat.class.class.class ! Class
  13. 13. Klazz = Class.new
 => Klazz puts Klazz
 => Klazz puts Klazz.class Class k = Klazz.new => #<Klazz:0x007f8fc121ab58>
  14. 14. Foo = Class.new do def bar "I'm bar...an instance method of foo" end end ! a = Foo.new p a.bar => "hello"
  15. 15. WOW!
  16. 16. SINGLETON METHODS We can add methods to a single instance
  17. 17. class Cat def meow; ’meow’; end end cat = Cat.new new_cat = Cat.new def cat.argh 'argh' end p cat.meow => "meow" p cat.argh => "argh" p new_cat.argh NoMethodError: undefined method `argh' for #<Cat:0x007fda63085878>
  18. 18. ? EIGENCLASS
  19. 19. THE OBJECT MODEL • Every object, classes included, has its own “real class,” be it a regular class or an eigenclass • The superclass of the eigenclass is the object class • The superclass of the eigenclass of a class is the eigenclass of the class’s superclass
  20. 20. THE OBJECT MODEL class C; end class D < C; end obj = D.new #Object C #C D obj Object #D #obj Metaprogramming Ruby - Pag. 125
  21. 21. WOW!
  22. 22. monkey patching “a way to extend or modify the runtime code of dynamic languages [...] without altering the original source code.”
  23. 23. class String def foo to_s + ' fooed' end end s = "hello" puts s.foo => "hello fooed"
  24. 24. REMOVE METHODS class String remove_method :to_s end
  25. 25. WOW!
  26. 26. duck typing When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck http://en.wikipedia.org/wiki/Duck_typing
  27. 27. class Type1 def foo; "I'm type1"; end end ! class Type2 def foo; "I'm type2"; end end def get_class rand < 0.5 ? Type1 : Type2 end !t = get_class.new p t.foo => "I'm type2" #or I'm type1
  28. 28. METHOD INTERCEPTION Using alias and monkey patching we can run through the execution
  29. 29. class def p end end cat = Cat meow "meow" Cat.new Cat.class_eval do alias :meow_new :meow def meow p "i'm about to meow" meow_new p "did you hear me?" end end => "i'm about to meow" => "meow" cat.meow => "did you hear me?"
  30. 30. METAPROGRAMMING Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data […] http://en.wikipedia.org/wiki/Metaprogramming
  31. 31. METHOD MISSING class Cat def method_missing(method, *args) #do something without failing end end
  32. 32. class Settings def initialize(options) options.each do |key, value| self.instance_variable_set "@#{key}", value self.class.send :define_method, key,
 proc{self.instance_variable_get("@#{key}")} self.class.send :define_method, "#{key}=",
 proc{|v| self.instance_variable_set("@#{key}", v)} end end end c = Settings.new YAML.load_file("config.yaml") ! p c.title # => "metaprogramming ruby" p c.author # => "Paolo Perrotta" p c.pub_year # => "2010" ! c.title = 'metaprogramming ruby 2.00’ p c.title # => "metaprogramming ruby 2.00”
  33. 33. HOOKS Since all code is executed. You can intercept some “facts” about it. inherited, append_features, included, extend_object, extended, initialize_copy, const_missing
  34. 34. WOW!
  35. 35. BUT THERE’S MORE > [1, 2, 3] * 3 == [1, 2, 3, 1, 2, 3, 1, 2, 3] > 1_000_000 > a = a || [] > a = [1,2,3]; a[-1] > [1,2,3].shuffle > (0..9).each { ... } > 3.times {...} > def name=(value); ...
  36. 36. PHILOSOPHY I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language. Yukihiro Matsumoto
  37. 37. o = Object.new! o.methods.count => 54 • ActiveRecord::Base => 367 • String => 161 • Fixnum => 128
  38. 38. SAME METRICS? DIFFERENT WORLD?
  39. 39. RUBY PRO • Simplicity • REPL (irb, rails c, heroku run console) • No ceremony • One file app • Community and frameworks • Expressiveness (DSL)
  40. 40. CONS • Freedom bring responsibility • Performance • No tools for refactoring • Tests are mandatory (mmh…)
  41. 41. DOES IT WORTH?
  42. 42. LINGUISTIC RELATIVITY “The principle of linguistic relativity holds that the structure of a language affects the ways in which its respective speakers conceptualize their world.” Sapir–Whorf hypothesis
 http://en.wikipedia.org/wiki/Linguistic_relativity
  43. 43. THE PRAGMATIC PROGRAMMER “Learn at least one new language every year. Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut.” The Pragmatic Programmer
  44. 44. MORE LANGUAGES 
 ==
 BETTER PROGRAMMER
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×