alexander dymo - it jam 2009 - ruby: beaty or the beast

Download Alexander Dymo - IT Jam 2009 - Ruby: Beaty Or The Beast

If you can't read please download the document

Upload: alexander-dymo

Post on 16-Apr-2017

1.029 views

Category:

Technology


2 download

TRANSCRIPT

Ruby: Beauty or the Beast?

IT Jam 2009

www.acunote.com

?

Ruby (Rails) Acunote www.acunote.com


, Scrum

2006-

22 000

31 000

~5600

?

Ruby

Garbage Collection

IDE

Ruby ?

Ruby:

!

~3 * ~4 = 22k ???

Ruby:

!

400 Ruby == 6000 C++

Ruby:

!module SearchLanguageclass Parser class ParseError < StandardError; end

def self.parse(text) parser = Parser.new parser.parse(text) end

private: def parse_search_query parse_and_expression end

def parse_and_expression match_spaces node = ExpressionNode.new(:and) return node if empty_buffer?

node.expressions 25

Ruby:

class BigDecimal

alias_method :eq_without_boolean_comparison, :==

def eq_with_boolean_comparison(other) if [FalseClass, TrueClass].include? other.class return false end eq_without_boolean_comparison(other) end

alias_method :==, :eq_with_boolean_comparisonend

Ruby:

class Time def humanize strftime("%a %b %d, %Y %H:%M") end end

... 26

Ruby:

/

class Foo def do_smth puts "Hello World" end end

class Foo def do_smth_with_decoration puts "--------------" do_smth_without_decoration puts "--------------" end end

f = Foo.new f.do_smth > -------------- > Hello World > --------------

Ruby:

class Foo { private: void doSmth() { std::cout Hi

?

Ruby

Garbage Collection

IDE

Ruby ?

Ruby:

Perl'

x = false or trueputs x #???

Ruby:

Perl'

x = false or true(x = false) or trueputs x> false

Ruby:

Perl'

x = false || truex = (false || true)puts x> true

Ruby:

Perl'

x = 2 if false or trueputs x> ?

Ruby:

Perl'

x = 2 if false or truex = 2 if (false or true)puts x> 2

Ruby:

Python'

class Fooattr_accessor :xdef do_smthx = 10endendf = Foo.newf.x> nil#WTF?

Ruby:

Python'

class Fooattr_accessor :xdef do_smthself.x = 10endendf = Foo.newf.x> 10

Ruby:

class Fooattr_accessor :x

def initialize@x = 10#self.x = 10y = @x#y = xendend

?

Ruby

Garbage Collection

IDE

Ruby ?

Ruby:

:

module ActiveRecord module ConnectionAdapters class MysqlAdapter def execute #... end end endend

module ActiveResource module Validations def valid? errors.empty? end endend

module ActiveRecord class Base include Validations endend

Ruby:

: /

def calculate(&algorithm) yield(2, 2)end

result = calculate do |x,y| x + yend

def calculate(&expression) return unless can_calculate? result = yield result.good? ? result : nilend

result = calculate do 2+2end

Ruby:

Rails: method_missing

class Userhas_many :roles end

user = User.find(:first)

# user.roles.find(:all, :conditions => "name = 'Admin'")

# user.roles.map { |role| role.name }

Ruby:

Rails: method_missing

class AssociationProxydef reload@target = [1,2,3] #just for an example end def method_missing(method, *args, &block)if @target.respond_to? [email protected](method, *args, &block)elseraise NoMethodErrorendendendend

p = AssociationProxy.new p.reload# p allocated: 1049602K total in 2052 allocations, GC calls: 146, GC time: 161 msec

Garbage Collection:

GC Ruby Enterprise Edition (www.rubyenterpriseedition.com)

-RUBY_HEAP_MIN_SLOTS=10000RUBY_HEAP_SLOTS_INCREMENT=10000RUBY_GC_MALLOC_LIMIT=8000000RUBY_HEAP_SLOTS_GROWTH_FACTOR=1.8

, RUBY_HEAP_MIN_SLOTS=1250000RUBY_HEAP_SLOTS_INCREMENT=100000RUBY_GC_MALLOC_LIMIT=30000000RUBY_HEAP_SLOTS_GROWTH_FACTOR=1

Garbage Collection:

?JRuby

Garbage Collection:

? generational garbage collector Ruby?

?

Ruby

Garbage Collection

IDE

Ruby ?

: IDE

Emacs (rinari, emacs-rails, ruby-mode v23)http://oldwiki.rubyonrails.org/rails/pages/HowToUseEmacsWithRails

RubyMinehttp://www.jetbrains.com/ruby/

VIMhttp://akitaonrails.com/2009/01/04/rails-on-vim-in-english

Textmatehttp://macromates.com/

KDevelop, Aptana, Netbeans, etc, etc...

: IDE

Ruby IDE

def foo(arg)arg.endfoo(2)foo(Class.new)

: IDE

Ruby IDE

class Foo10.times do |i|define_method "great_method_#{i}".to_sym do#...endendendf = Foo.newf.

?

Ruby

Garbage Collection

IDE

Ruby ?

:

TDD/BDD: Test::Unit, RSpec, Cucumber, etc.

Continuous Integration: CruiseControl (http://cruisecontrol.sourceforge.net/)

:

Test::Unit,

:

def test_file_treetree = FilesTree.new("/home/user")assert treeassert_equal ".", tree[0], assert_equal "..", tree[1],assert_equal "dir1", tree[2]assert_equal "file1", tree[2][0]assert_equal "file2", tree[3]end

:

Test::Unit, :

(cucumber)

scenario: sample file treeWhen I go to /home/userThen I see current directory, top directory, dir1There's a file2 in current directoryThere's a file1 in dir1 directoryend

:

Test::Unit, :

(Test::Unit):

def test_file_treetree = FilesTree.new("/home/user")assert_equal "...dir1file1file2", serialize_tree(tree)end

?

Ruby

Garbage Collection

IDE

Ruby ?

:

Ruby , , ... ?

:

Ruby !

:

Ruby !

:

2008

2008

2008

2008

, %

: O(na)

: O(log n)

:

?

C SQL

:

Ruby-Prof (http://ruby-prof.rubyforge.org/)

:

%self total self child calls name 8.39 0.54 0.23 0.31 602 Array#each_index 7.30 0.41 0.20 0.21 1227 Integer#gcd 6.20 0.49 0.17 0.32 5760 Timecell#date 5.11 0.15 0.14 0.01 1 Magick::Image#to_blob

gem install ruby-profKCachegrind http://kcachegrind.sourceforge.net

:

:

? :

%self total self child calls name 1.42 0.39 0.25 0.14 1033 Range#each-1 0.85 0.43 0.15 0.28 2221 Array#each-1 0.74 0.13 0.13 0.00 113447 Array#[] 0.40 0.07 0.07 0.00 5008 Hash#initialize_copy 0.40 0.07 0.07 0.00 45467 Hash#[]= 0.40 0.13 0.07 0.06 37784 Hash#[] 0.17 0.15 0.03 0.12 176 Array#collect 0.17 0.08 0.03 0.05 22371 String#gsub 0.17 0.03 0.03 0.00 44881 Array#