Alexander Dymo - IT-клуб Николаева - April 2011 - Ruby: Beaty and the Beast

Embed Size (px)

Citation preview

Ruby: Beauty and the Beast

IT-

www.acunote.com

?

Ruby (+Rails)
-Acunote www.acunote.com

2006-

29 000

45 500

~12000

scrum,

?

Ruby

Ruby Runtime

Garbage Collection

IDE

Ruby ?

Ruby:

!

~3-4 * ~5 = 29k ???

5-7

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 Time def humanize strftime("%a %b %d, %Y %H:%M") end end

puts Time.now.humanize > Thu Apr 21, 2011 13:37

... 28

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

Ruby Runtime

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

Ruby Runtime

Garbage Collection

IDE

Ruby ?

Ruby:

:

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

module ActiveRecord module Validations def valid? errors.empty? end end

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:

method_missing

class GoldenFishdef method_missing(name, *args, &block)if name == :make_me_richputs "here you go: $1 000 000"elseputs "you want too much!"endend end

fish = GoldenFish.newfish.make_me_rich> here you go: $1 000 000fish.make_my_old_wife_a_supermodel> you want too much!

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: 17281 msec

Ruby Runtime: Garbage Collection

, GChttp://blog.pluron.com/2008/02/memory-profilin.html

Ruby Runtime: Garbage Collection

:

GCGCArea Burndown 1200,940,651,5xSprint 20 x (1+5) (C)0,590,302,0xSprint 20 x (1+5)0,700,401,8xMove Left0,770,461,7x

Ruby 2 !"" !

Ruby Runtime: 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

Ruby Runtime: Garbage Collection

?JRuby

Ruby Runtime: Garbage Collection

? generational garbage collector Ruby?

?

Ruby

Ruby Runtime

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

Ruby Runtime

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 serialize_tree(tree),