Ruby for .NET developers


Published on

Introduction to Ruby for .NET developers.
Learn and compare

Published in: Technology, Education
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Ruby for .NET developers

  1. 1. Ruby for .NETDevelopers By Max Titov Ninja Software Operations
  2. 2. Objective: learn and compare▶ What is Ruby?▶ Ruby basics▶ Ruby specialties▶ Ruby ecosystem▶ So why Ruby?▶ How to get started?
  3. 3. What is Ruby?
  4. 4. Creator"I wanted a scripting language that wasmore powerful than Perl, and more object-oriented than Python. Thats why I decidedto design my own language.” Yukihiro (Matz) Matsumoto
  5. 5. Facts▶ First “Hello World” in 1995 (.NET 2002, C# 2001)▶ Ruby is opensource▶ Inspired by: Perl, Smalltalk, Lisp, Python …▶ Philosophy: Designed for programmer productivity and fun.
  6. 6. Ruby Basics
  7. 7. First taste of Ruby codeclass Apple NAME = "Apple" attr_accessor :size, :color def initialize size @size = size end def taste puts "Sweet #{@color} #{NAME} of size #{size}" endendapple = bigapple.color = redapple.taste # Sweet red Apple of size big
  8. 8. I know, feels like
  9. 9. Similarities▶ Large standard library (Not so big as .NET Framework but feels enough)▶ The are classes, methods, variables, properties.▶ Access control modifiers▶ Closures (Lambdas)▶ Exceptions▶ Garbage collector
  10. 10. Ruby is Dynamic▶ No need to declare variablesvar = "Ruby is Dynamic"var.class #Stringvar = 1var.class #Fixnum
  11. 11. Ruby is Strong Typed▶ Like in .NET there is no type juggling. You need to convert between types.a = "1"b=2a + b #TypeError: can`t convert Fixnum into Stringa.to_i + b # 3
  12. 12. Everything is an Object▶ All classes are drived from base class named Class▶ Unlike .NET there is no structs
  13. 13. Everything is an Object▶ So even primitive Types are an objects10.times {puts "I am sexy and I know it!"}# I am sexy and I know it!# I am sexy and I know it!# I am sexy and I know it!# I am sexy and I know it!# I am sexy and I know it!# ....(10 times)....
  14. 14. Everything is an Object▶ Operators are simply object methods.class Fixnum < Integer def + numeric # sum code endend
  15. 15. Ruby is Flexible▶ Core Ruby code could be easy altered.class Numeric def toSquare self * self endend2.toSquare # 4
  16. 16. Ruby is Concise▶ Properties could be defined in old school wayclass Person #getter def name @name end #setter def name= name @name = name endend
  17. 17. Ruby is Concise▶ Or in more convenient styleclass Person #getter and setter, for several properties attr_accessor :name , :nickname #getter attr_reader :gender #setter attr_writer :ageend
  18. 18. Some questions to you▶ Constants, start from capital or not?▶ Field names, prefixed with underscore or not?▶ How many coding guide lines is there actually? ▶ Microsoft Framework Design Guidelines ▶ IDesign C# coding standards ▶ Your company coding standard ▶ Your own coding standard. (Professional choice)
  19. 19. Ruby is Strict▶ Autocracy took over the Ruby community.
  20. 20. Ruby is StrictRuby syntaxes mostly dictates namingconventions: ▶ localVariable ▶ @instanceVariable ▶ @@classVariable ▶ $globalVariable ▶ Constant ▶ ClassName ▶ method_name
  21. 21. Ruby is Strict▶ 95% of ruby developers use same code style.▶ Other 5% are a new comers, that will adept code conventions soon.
  22. 22. So in Ruby world you don’t feel like:Forever alone in the world of naming conventions.
  23. 23. And Ruby Is Forgiving▶ Parenthesis are optional▶ No need in semicolon at the end of each line
  24. 24. Rubyspecialties
  25. 25. Duck typing What really makes object an object?How can I recognize that object is a Duck?
  26. 26. Duck typingBehavior
  27. 27. Duck typing▶ Definition: 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. (Wikipedia)
  28. 28. So, is it a duck?Swim? YesCan Quack? YesIs it a duck?Definitely!
  29. 29. And this?Swim? YesCan Quack? Yes. Kind ofstrange, but still itmake quack like soundIs it a duck?Looks like!
  30. 30. How, about this?Swim? Badly, but yes.Can Quack? Yeah, makePlenty of sounds but, canquack also.Is it a duck?Sort of weird duck, but stillyes!
  31. 31. Or, probably this?Swim? YepCan quack? Canmake weird quacksounds.Is it duck?Trying very hard
  32. 32. Duck Typing▶ So, everything that could respond to several criterias that makes us believe that object is a duck, can be recognized as a duck.▶ But what that means from programmer perspective and how to implement it?
  33. 33. What is told you there is noabstract classes and interfaces?
  34. 34. But there is Modules and Mixins!▶ Modules define pieces of reusable code that couldn’t be instantiated.▶ Modules provides a namespace functionality and prevent name clashes
  35. 35. Namespaces in Rubymodule System module Windows module Forms module MessageBox def MessageBox.Show message puts message end end end endendinclude System::Windows::FormsMessageBox.Show Namespacing in ruby’
  36. 36. Modules and Mixins▶ Modules could be “mixed in” to any class that satisfy conventions described in documentation (Should quack and swim like a duck).▶ In .net Mixins using ReMix
  37. 37. Lets see how it works byimplementing Enumerable
  38. 38. In .NET we usually do this▶ We need to implement two interfaces ▶ IEnumerable ▶ IEnumerator
  39. 39. In .NET we usually do thisclass People : IEnumerable{ IEnumerator GetEnumerator() { return (IEnumerator) new PeopleEnumerator(); }}public class PeopleEnumerator : IEnumerator{ public Person Current; public void Reset(); public bool MoveNext();}public class Person{}
  40. 40. How it’s done in Ruby▶ From Enumerable module documentation: The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The client class must provide a method “each”, which yields successive members of the collection.
  41. 41. How it’s done in Rubyclass MyCollection include Enumerable def each #yields result endend
  42. 42. That was easy!
  43. 43. But static typing and interfaces make me safe! Really?
  44. 44. In Ruby world developers used to write unit tests for this
  45. 45. Document and organize their code better# The <code>Enumerable</code> mixin provides collection classes with# several traversal and searching methods, and with the ability to# sort. The class must provide a method <code>each</code>, which# yields successive members of the collection. If# <code>Enumerable#max</code>, <code>#min</code>, or# <code>#sort</code> is used, the objects in the collection must also# implement a meaningful <code><=></code> operator, as these methods# rely on an ordering between members of the collection.module Enumerable # enum.to_a -> array # enum.entries -> array # Returns an array containing the items in <i>enum</i>. # # (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7] # { a=>1, b=>2, c=>3 }.to_a #=> [["a", 1], ["b", 2], # ["c", 3]] def to_a() #This is a stub, used for indexing end
  46. 46. Closures in Ruby▶ Closures in Ruby called Blocksnames = ["Max", "Alex", "Dima"].map do |name| name.downcaseendputs names# max# alex# dima
  47. 47. Ruby metaprogramming▶ Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime. (Wikipedia)▶ Keep programs DRY – Don’t repeat yourself.
  48. 48. Where is example?In all cinemas of your town Next time “Ruby metaprogramming”
  49. 49. RubyEcosystem
  50. 50. FrameworksRuby .NET▶ Ruby on ▶ ASP.NET Rails, Merb MVC, FunuMVC▶ Sinatra ▶ Nancy▶ Radiant, Mephisto ▶ Umbraco, DotNetN uke
  51. 51. ToolsRuby .NET▶ Any TextEditor ▶ Visual (RubyMine IDE) Studio, MonoDevel op▶ Rake ▶ MSBuild, NAnt▶ Gems ▶ Dll’s▶ Gems and Bundler ▶ NuGet▶ TestUnit, minitest ▶ MSUnit, NUnit …▶ Cucumber, RSpec, ▶ NSpec, SpecFlow Shoulda
  52. 52. So WhyRuby?
  53. 53. So Why Ruby?▶ All hot stuff is here ▶ Benefits of interpreted language▶ Quick prototyping with Rails▶ It’s fun and it’s going to make your better!▶ And definitely it will sabotage what you believe in.
  54. 54. Feel more Rubier now? I hope so 
  55. 55. Ruby tutorial 101Interactive Ruby tutorial:▶ course:▶
  56. 56. Books▶ Programming Ruby (Pick Axe book)By Thomas D., Fowler C., Hunt A.▶ Design Patterns In RubyBy Russ Olsen▶ Search Google for: Learn Ruby
  57. 57. Follow the ruby side we have cookies 
  58. 58. Yep, we really do! 
  59. 59. Questions? Ruby for .NET developers By Max TitovGet presentation: Get in touch: Twitter: eolexe