Created
November 4, 2012 06:25
-
-
Save dabit/4010563 to your computer and use it in GitHub Desktop.
Ruby 2.0 features examples
This file contains 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
elements = (1..19999999) | |
p elements.class # => Range | |
results = elements.select { |n| | |
n % 2 == 0 | |
}.map { |n| | |
n + 1 | |
}.take(10) | |
p results # => [3, 5, 7, 9, 11, 13, 15, 17, 19, 21] | |
# ruby 2.72s user 0.10s system 99% cpu 2.822 total |
This file contains 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
elements = (1..19999999).lazy | |
p elements.class # => Enumerator::Lazy | |
results = elements.select { |n| | |
n % 2 == 0 | |
}.map { |n| | |
n + 1 | |
}.take(10) | |
p results.force # => [3, 5, 7, 9, 11, 13, 15, 17, 19, 21] | |
# ruby 0.04s user 0.04s system 96% cpu 0.087 total |
This file contains 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
module FooBar | |
def say | |
puts "2 - Module" | |
super | |
end | |
end | |
class Foo | |
def say | |
puts "3 - Parent Class" | |
end | |
end | |
class Bar < Foo | |
include FooBar | |
def say | |
puts "1 - Implementing Class" | |
super | |
end | |
end | |
Bar.new.say # => | |
# 1 - Implementing Class | |
# 2 - Module | |
# 3 - Parent Class |
This file contains 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
module FooBar | |
def say | |
puts "2 - Module" | |
super | |
end | |
end | |
class Foo | |
def say | |
puts "3 - Parent Class" | |
end | |
end | |
class Bar < Foo | |
prepend FooBar | |
def say | |
puts "1 - Implementing Class" | |
super | |
end | |
end | |
Bar.new.say # => | |
# 2 - Module | |
# 1 - Implementing Class | |
# 3 - Parent Class |
This file contains 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 String | |
def to_s | |
self + "......" | |
end | |
end | |
puts "No periods".to_s # => "No periods......" |
This file contains 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
module MonkeyPatch | |
refine String do | |
def to_s | |
self + "......" | |
end | |
end | |
end | |
puts "No periods".to_s # => "No periods" | |
class SomeController | |
using MonkeyPatch | |
puts "No periods".to_s # => "No periods......" | |
end | |
puts "No periods".to_s # => "No periods" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment