ruby seen by a c# developer
DESCRIPTION
What an expert and deep-rooted C# developer discover about Ruby. Here are the impressions, the oddities and the WOWs.TRANSCRIPT
![Page 1: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/1.jpg)
RUBY SEEN BY A C# DEVELOPEREmanuele DelBono - @emadb
![Page 2: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/2.jpg)
![Page 3: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/3.jpg)
![Page 4: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/4.jpg)
![Page 5: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/5.jpg)
![Page 6: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/6.jpg)
![Page 7: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/7.jpg)
![Page 8: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/8.jpg)
![Page 9: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/9.jpg)
I AM
I am a software engineer and web architect in CodicePlastico. I write web apps in C#, javascript e ruby.
![Page 10: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/10.jpg)
IT ISN’T A RUBY TUTORIAL!
It’s a bunch of stuff that made me say WOW!
![Page 11: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/11.jpg)
C# (2002)!
• Compiled, strongly typed
• OOP (multiparadigm)
• Delegate/lambda/enumerators
• ECMA standard
• Mono on Linux and MacOS
• “A better java”
![Page 12: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/12.jpg)
RUBY (1995)
• Dynamic
• Open Source
• Interpreted
• Multi inheritance through Mixin
• Fully OOP
![Page 13: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/13.jpg)
HELLO WORLD
using System;!public class Program {! public void Main() {! Console.WriteLine("Hello World"); }!}!
![Page 14: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/14.jpg)
HELLO WORLD
p 'hello world'
![Page 15: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/15.jpg)
![Page 16: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/16.jpg)
RAILSclass 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
![Page 17: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/17.jpg)
ALL CODE IS EXECUTED
class MyClass < (rand < 0.5 ? Array : Hash)!end!
![Page 18: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/18.jpg)
WOW!
![Page 19: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/19.jpg)
REAL OOP
class is an instance of class
![Page 20: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/20.jpg)
class Cat end cat = Cat.new
puts cat.class.class.class !Class
puts cat #<Cat:0x007fa2bc13d5d8>
puts cat.class Cat
puts cat.class.class Class
![Page 21: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/21.jpg)
Klazz = Class.new=> Klazz
puts Klazz=> Klazz
puts Klazz.class Class
k = Klazz.new => #<Klazz:0x007f8fc121ab58>
![Page 22: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/22.jpg)
Foo = Class.new do def bar "I'm bar...an instance method of foo" end end !a = Foo.new p a.bar => "hello"
![Page 23: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/23.jpg)
WOW!
![Page 24: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/24.jpg)
SINGLETON METHODS
We can add methods to a single instance
![Page 25: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/25.jpg)
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>
![Page 26: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/26.jpg)
?EIGENCLASS
![Page 27: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/27.jpg)
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
![Page 28: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/28.jpg)
THE OBJECT MODEL
obj #obj
D
C
Object
#D
#C
#Object
Metaprogramming Ruby - Pag. 125
class C; end class D < C; end obj = D.new
![Page 29: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/29.jpg)
WOW!
![Page 30: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/30.jpg)
monkey patching“a way to extend or modify the runtime code of dynamic languages [...] without
altering the original source code.”
![Page 31: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/31.jpg)
class String def foo to_s + ' fooed' end end
s = "hello"
puts s.foo=> "hello fooed"
![Page 32: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/32.jpg)
REMOVE METHODS
class String remove_method :to_s end
![Page 33: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/33.jpg)
WOW!
![Page 34: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/34.jpg)
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
![Page 35: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/35.jpg)
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
![Page 36: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/36.jpg)
METHOD INTERCEPTION
Using alias and monkey patching we can run through the execution
![Page 37: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/37.jpg)
class Cat def meow p "meow" end end cat = 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
cat.meow
=> "i'm about to meow" => "meow" => "did you hear me?"
![Page 38: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/38.jpg)
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
![Page 39: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/39.jpg)
class Cat def method_missing(method, *args) #do something without failing end end
METHOD MISSING
![Page 40: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/40.jpg)
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 endc = 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”
![Page 41: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/41.jpg)
HOOKS
Since all code is executed. You can intercept some “facts” about it.
inherited, append_features, included, extend_object, extended, initialize_copy,
const_missing
![Page 42: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/42.jpg)
WOW!
![Page 43: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/43.jpg)
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); ...
![Page 44: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/44.jpg)
PHILOSOPHYI 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
![Page 45: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/45.jpg)
o = Object.new!o.methods.count
=> 54
• ActiveRecord::Base => 367
• String => 161
• Fixnum => 128
![Page 46: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/46.jpg)
SAME METRICS?
DIFFERENT WORLD?
![Page 47: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/47.jpg)
RUBY PRO
• Simplicity
• REPL (irb, rails c, heroku run console)
• No ceremony
• One file app
• Community and frameworks
• Expressiveness (DSL)
![Page 48: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/48.jpg)
CONS
• Freedom bring responsibility
• Performance
• No tools for refactoring
• Tests are mandatory (mmh…)
![Page 49: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/49.jpg)
DOES IT WORTH?
![Page 50: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/50.jpg)
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
![Page 51: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/51.jpg)
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
![Page 52: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/52.jpg)
MORE LANGUAGES ==
BETTER PROGRAMMER
![Page 53: Ruby seen by a C# developer](https://reader034.vdocument.in/reader034/viewer/2022051411/540df3638d7f728d7e8b4b7f/html5/thumbnails/53.jpg)