Reasons To Love Ruby

Post on 31-May-2015

1,447 views 2 download

Tags:

description

A general introduction to the Ruby language and ecosystem from a .NET Developer's perspective.

transcript

a bunch of reasons to love ruby

class Ben < Person is_a :mvp, :scrummaster, :aspinsider

def initialize@book = “ASP.NET MVC in Action”@twitter = “subdigital”@blog = “http://flux88.com”@iphone_app = “Pocket Tabs”

end

...

end

def self.expert?false

end

Meet Ruby

puts “hello world” hello world

puts “hello world”.reverse dlrow olleh

puts “hello world”[0..5] hello

Ruby Variables

num = 323

message = “Hey there”

stuff = [1, “foo”, true]

Ruby Loops

[“cat”, “dog”, “giraffe”].each do |s|puts s

end

[“cat”, “dog”, “giraffe”].each { |s| puts s }

Working with Arrays

animals = [“zebra”, “donkey”, “lemur”]

animals.sort!

# original array is untouched

# original array is modified!

animals.sort [“donkey”, “lemur”, “zebra”]

animals.delete “zebra” [“donkey”, “lemur”]

Ruby has Less Ceremony

//C#Dictionary<string, object> hash = new

Dictionary<string, object>();

#rubyhash = {}

#optional semicolons & parentheses

add(5, 6); is the same as add 5, 6

You Can Extend Existing classes

5.days.from_now

Ruby is Terse, yet Readable

3.times { print “Ho ” } => Ho Ho Ho

approve_credit unless account.deliquent?

send_email if email_enabled?

Regex is a 1st class citizen

“The Times They are a-Changin’”.gsub /\s/, “.” The.Times.They.are.a-Changin’

[“red”, “blue”, “green”].grep /u/ blue

Everything is an Object

No, really… everything is an object

42.class FixNum

42.2.class.superclass Integer

42.class.superclass.superclass Numeric

42.class.superclass.superclass.superclass Object

puts customer.name unless customer.nil?

nil.class NilClass

nil.class.superclass Object

RubyGems

gem install hpricot

Speaking of hpricot…

require ‘rubygems’require ‘hpricot’require ‘open-uri’

uri = open(“http://whatsit.org”)doc = Hpricot(uri)

(doc/"p/a/img").each do |img| puts img.attributes['class']end

Rake

task :codeGen do # do the code generationend

task :compile => :codeGen do #do the compilationend

task :dataLoad => :codeGen do # load the test dataend

task :test => [:compile, :dataLoad] do # run the testsend

RSpec

# bowling_spec.rbrequire 'bowling'

describe Bowling do it "should score 0 for gutter game" do bowling = Bowling.new 20.times { bowling.hit 0 } bowling.score.should == 0 endend

…and now the elephant in the room

rails

Rails is the bee’s knees because…

convention

configuration

Rails is the bee’s knees because…

it hasActiveRecord

Rails is the bee’s knees because…

it hasMigrations

Rails is the bee’s knees because…

it has theConsole

Rails is the bee’s knees because…

scaffolding

Rails is the bee’s knees because…

Logging is built-in

Rails is the bee’s knees because…

Tests come out of the box

Rails is the bee’s knees because…

There is ONE WAY

What else?

heroku

What else?

IronRuby

thank_you