+ All Categories
Home > Technology > Ruby for .NET developers

Ruby for .NET developers

Date post: 13-May-2015
Category:
Upload: max-titov
View: 3,509 times
Download: 3 times
Share this document with a friend
Description:
Introduction to Ruby for .NET developers.Learn and compare
Popular Tags:
60
By Max Titov maxtitov.me Ninja Software Operations Ruby for .NET Developers VS FOR
Transcript
Page 1: Ruby for .NET developers

By Max Titovmaxtitov.me

Ninja Software Operations

Ruby for .NETDevelopers VS FOR

Page 2: Ruby for .NET developers

Objective: learn and compare

▶ What is Ruby? ▶ Ruby basics▶ Ruby specialties▶ Ruby ecosystem▶ So why Ruby?▶ How to get started?

Page 3: Ruby for .NET developers
Page 4: Ruby for .NET developers

What is Ruby?

Page 5: Ruby for .NET developers

Creator

"I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language.”

Yukihiro (Matz) Matsumoto

Page 6: Ruby for .NET developers

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.

Page 7: Ruby for .NET developers

Ruby Basics

Page 8: Ruby for .NET developers

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}" endend

apple = Apple.new 'big'apple.color = 'red' apple.taste # Sweet red Apple of size big

Page 9: Ruby for .NET developers

I know, feels like

Page 10: Ruby for .NET developers

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

Page 11: Ruby for .NET developers

Ruby is Dynamic

▶ No need to declare variables

var = "Ruby is Dynamic"var.class #Stringvar = 1var.class #Fixnum

Page 12: Ruby for .NET developers

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

Page 13: Ruby for .NET developers

Everything is an Object

▶ All classes are drived from base class named Class

▶ Unlike .NET there is no structs

Page 14: Ruby for .NET developers

Everything is an Object

▶ So even primitive Types are an objects

10.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)....

Page 15: Ruby for .NET developers

Everything is an Object

▶ Operators are simply object methods.

class Fixnum < Integer def + numeric # sum code endend

Page 16: Ruby for .NET developers

Ruby is Flexible

▶ Core Ruby code could be easy altered.

class Numeric def toSquare self * self endend

2.toSquare # 4

Page 17: Ruby for .NET developers

Ruby is Concise

▶ Properties could be defined in old school way

class Person #getter def name @name end

#setter def name= name @name = name endend

Page 18: Ruby for .NET developers

Ruby is Concise

▶ Or in more convenient style

class Person #getter and setter, for several properties attr_accessor :name , :nickname

#getter attr_reader :gender

#setter attr_writer :ageend

Page 19: Ruby for .NET developers

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)

Page 20: Ruby for .NET developers

Ruby is Strict

▶ Autocracy took over the Ruby community.

Page 21: Ruby for .NET developers

Ruby is Strict

Ruby syntaxes mostly dictates naming conventions:

▶ localVariable▶ @instanceVariable▶ @@classVariable▶ $globalVariable▶ Constant ▶ ClassName▶ method_name

Page 22: Ruby for .NET developers

Ruby is Strict

▶ 95% of ruby developers use same code style.

▶ Other 5% are a new comers, that will adept code conventions soon.

Page 23: Ruby for .NET developers

Forever alone in the world of naming conventions.

So in Ruby world you don’t feel like:

Page 24: Ruby for .NET developers

And Ruby Is Forgiving

▶ Parenthesis are optional▶ No need in semicolon at the end of

each line

Page 25: Ruby for .NET developers

Ruby specialties

Page 26: Ruby for .NET developers

Duck typing

What really makes object an object?How can I recognize that object is a

Duck?

Page 27: Ruby for .NET developers

Duck typing

Behavior

Page 28: Ruby for .NET developers

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)

Page 29: Ruby for .NET developers

So, is it a duck?

Swim? YesCan Quack? Yes

Is it a duck?Definitely!

Page 30: Ruby for .NET developers

And this?

Swim? YesCan Quack? Yes. Kind of strange, but still it make quack like sound

Is it a duck?Looks like!

Page 31: Ruby for .NET developers

How, about this?

Swim? Badly, but yes. Can Quack? Yeah, make Plenty of sounds but, can quack also.

Is it a duck?Sort of weird duck, but still yes!

Page 32: Ruby for .NET developers

Or, probably this?

Swim? YepCan quack? Can make weird quacksounds.

Is it duck?Trying very hard

Page 33: Ruby for .NET developers

Duck Typing

▶ So, everything that could respond to several criteria's that makes us believethat object is a duck, can be recognized as a duck.

▶ But what that means from programmer perspective and how to implement it?

Page 34: Ruby for .NET developers

What is told you there is no abstract classes and interfaces?

Page 35: Ruby for .NET developers

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

Page 36: Ruby for .NET developers

Namespaces in Rubymodule System module Windows module Forms module MessageBox def MessageBox.Show message puts message

end end end endend

include System::Windows::FormsMessageBox.Show 'Namespacing in ruby’

Page 37: Ruby for .NET developers

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 http://remix.codeplex.com/

Page 38: Ruby for .NET developers

Lets see how it works by implementing Enumerable

Page 39: Ruby for .NET developers

In .NET we usually do this

▶ We need to implement two interfaces▶ IEnumerable▶ IEnumerator

Page 40: Ruby for .NET developers

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{}

Page 41: Ruby for .NET developers

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.

Page 42: Ruby for .NET developers

How it’s done in Ruby

class MyCollection include Enumerable def each #yields result endend

Page 43: Ruby for .NET developers

That was easy!

Page 44: Ruby for .NET developers

But static typing and interfaces make me safe!

Really?

Page 45: Ruby for .NET developers

In Ruby world developers used to write unit tests for this

Page 46: Ruby for .NET developers

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

Page 47: Ruby for .NET developers

Closures in Ruby

▶ Closures in Ruby called Blocks

names = ["Max", "Alex", "Dima"].map do |name| name.downcaseendputs names# max# alex# dima

Page 48: Ruby for .NET developers

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.

Page 49: Ruby for .NET developers

Where is example?

In all cinemas of your town Next time “Ruby

metaprogramming”

Page 50: Ruby for .NET developers

Ruby Ecosystem

Page 51: Ruby for .NET developers

Frameworks

Ruby

▶ Ruby on Rails, Merb

▶ Sinatra▶ Radiant,

Mephisto

.NET

▶ ASP.NET MVC, FunuMVC

▶ Nancy ▶ Umbraco,

DotNetNuke

Page 52: Ruby for .NET developers

Tools

Ruby

▶ Any TextEditor (RubyMine IDE)

▶ Rake▶ Gems ▶ Gems and

Bundler ▶ TestUnit,

minitest▶ Cucumber,

RSpec, Shoulda

.NET

▶ Visual Studio, MonoDevelop

▶ MSBuild, NAnt▶ Dll’s▶ NuGet▶ MSUnit, NUnit … ▶ NSpec, SpecFlow

Page 53: Ruby for .NET developers

So Why Ruby?

Page 54: Ruby for .NET developers

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.

Page 55: Ruby for .NET developers

Feel more Rubier now? I hope so

Page 56: Ruby for .NET developers

Ruby tutorial 101

Interactive Ruby tutorial:▶ http://tryruby.org/

Online course:▶ http://www.coursera.org/course/saas/

Page 57: Ruby for .NET developers

Books

▶Programming Ruby (Pick Axe book)By Thomas D., Fowler C., Hunt A.

▶Design Patterns In RubyBy Russ Olsen

▶Search Google for: Learn Ruby

Page 58: Ruby for .NET developers

Follow the ruby side we have

cookies

Page 59: Ruby for .NET developers

Yep, we really do!

Page 60: Ruby for .NET developers

Questions?Ruby for .NET developers

By Max TitovGet presentation: www.maxtitov.me

Get in touch: [email protected]: eolexe


Recommended