Created
March 1, 2013 18:11
-
-
Save vinirodr/5066566 to your computer and use it in GitHub Desktop.
some ruby 2.0 features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby 1.9 - Argumentos Ordenados | |
def params_1_9(name, age) | |
name = args[:name] | |
age = args[:age] | |
puts name | |
puts age | |
end | |
# ruby 2.0 - Argumentos Nomeados | |
def params_2_0(name: "nome default", age: 20) | |
puts name | |
puts age | |
end | |
params_2_0 age: 12, name: "goten" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby 2.0 | |
array = [0, 4, 7, 10, 12] | |
puts array.bsearch {|x| x >= 6 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby 1.9 | |
puts File.dirname(File.realpath(__FILE__)) | |
# ruby 2.0 | |
puts __dir__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def double_splat(name: "nome default", age: 20, **catched) | |
puts name | |
puts age | |
puts catched | |
end | |
double_splat name: "goku", age: 44, level: "saiyajin 7", ki: "890" | |
p method(:double_splat).parameters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Wikipedia | |
DTrace can be used to get a global overview of a running system, | |
such as the amount of memory, CPU time, filesystem and network resources | |
used by the active processes. | |
It can also provide much more fine-grained information, | |
such as a log of the arguments with which a specific function is being called, | |
or a list of the processes accessing a specific file. | |
Linguagens: | |
Assembly language, C, C++, Java, JavaScript, Perl, PHP, Python, Shell e RUBY :DDD | |
Infos sobre: | |
Fluxo de execução | |
Análise performance em tempo de execução | |
Memória, kernel,.. | |
Garbage collector | |
Google Talk sobre DTrace: http://www.youtube.com/watch?v=TgmA48fILq8&feature=gv | |
=================================================================================================== | |
[root@dn10 dtrace]$cat rb_funcalls.d | |
#!/usr/sbin/dtrace -Zqs | |
/\* | |
\* How do you invoke this script? | |
\* Use dtrace -xbufsize=40m -Zqs rb_funcall.d -c "ruby my_arg1 my_arg2" | |
\* Where "ruby" is DTrace enabled. | |
\*/ | |
/\* | |
\* Thread local variable to | |
\* store the indentation level | |
\* during/after a function call. | |
\*/ | |
self int indent; | |
dtrace:::BEGIN | |
{ | |
printf("Starting to trace\\n"); | |
} | |
ruby\*:::function-entry | |
{ | |
/\* | |
\* To ensure that we capture function | |
\* entries and returnes made by a thread | |
\* in order. | |
\*/ | |
self->thread = 1; | |
} | |
ruby\*:::function-entry | |
/self->thread/ | |
{ | |
self->indent += 2; | |
printf("%\*s", self->indent, " "); | |
printf("=> %s->%s\\n", copyinstr(arg0), copyinstr(arg1)); | |
} | |
ruby\*:::function-return | |
/self->thread/ | |
{ | |
printf("%\*s", self->indent, " "); | |
printf("<= %s->%s\\n", copyinstr(arg0), copyinstr(arg1)); | |
self->indent -= 2; | |
} | |
dtrace:::END | |
{ | |
printf("Finished tracing\\n"); | |
} | |
[root@dn10 dtrace]$ | |
[root@dn10 dtrace]$cat /tmp/hello.rb | |
message = String.new("Hello World") | |
puts message | |
[root@dn10 dtrace]$ | |
[root@dn10 dtrace]$dtrace -xbufsize=40m -Zqs rb_funcalls.d -c "ruby /tmp/hello.rb" | |
Starting to trace | |
Hello World | |
=> Class->new | |
=> String->initialize | |
<= String->initialize | |
<= Class->new | |
=> Object->puts | |
=> IO->write | |
<= IO->write | |
=> IO->write | |
<= IO->write | |
<= Object->puts | |
Finished tracing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Time | |
def melhor_do_br | |
puts "Fluminense" | |
end | |
end | |
Time.new.melhor_do_br | |
class Time | |
method_for_change = instance_method :melhor_do_br | |
define_method :melhor_do_br do | |
method_for_change.bind(self).call | |
puts "será melhor da américa em 2013" | |
end | |
end | |
Time.new.melhor_do_br |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Time | |
def melhor_do_br | |
puts "Fluminense" | |
end | |
end | |
Time.new.melhor_do_br | |
class Time | |
alias_method :fodão_do_br, :melhor_do_br | |
def melhor_do_br | |
fodão_do_br | |
puts "será melhor da américa em 2013" | |
end | |
end | |
Time.new.melhor_do_br |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* Expressões regulares com onigumo | |
* Refinements (restrição da total abertura de classes do ruby, ainda não está 100% quanto sua implementação) | |
* Novos métodos da classe Enumerators | |
* Mais uma porrada de coisa que a gente provavelmente não precisa saber agora | |
* Ler post do Nando! | |
* Documento com todas as modificações https://github.com/ruby/ruby/blob/ruby_2_0_0/NEWS | |
* Instalação | |
cd ~/.rbenv/plugins/ruby-build | |
git pull | |
rbenv install 2.0.0-p0 | |
rbenv local 2.0.0-p0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ruby 1.9 | |
p %w[simba timão pumba] | |
# ruby 2.0 | |
p %i[simba timão pumba] | |
king = :mufasa | |
p %I[simba timão pumba #{king}] | |
puts __ENCODING__ | |
# ruby 1.9 era US-ASCII |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# to_h no classe OpenStruct | |
require "ostruct" | |
warrior = OpenStruct.new(name: "goku", level: "saiyajin 7") | |
puts warrior | |
puts warrior.to_h | |
w = warrior.to_h | |
puts w[:name] | |
# to_h no classe Struct | |
Warrior = Struct.new(:name, :level) | |
warrior2 = Warrior.new("gohan", "saiyajin 2") | |
puts warrior2 | |
puts warrior2.to_h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ruby -c -w | |
var1 = 1 | |
var2 = 2 | |
_var3 = 3 | |
puts var1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment