Gabor Ratky / @rgabo
rvm install ruby-2.0.0
rbenv install 2.0.0-p353
def foo(foo: 'bar', baz: 'qux', **rest)
# TODO
end
foo(baz: 'qux', foo: 'frob')
def foo(optional: 'default', required:)
end
module Foo
def somehing; end
end
class MyClass
prepend Foo
end
MyClass.ancestors # =>[Foo, MyClass, Object...]
No more alias_method_chain
to_infinity = (0..Float::Infinity)
beyond =to_infinity.lazy.select do |n|
num % 42 == 0
end
100.times do { |n| puts beyond.next }
(1..100).to_a.permutation(4).size # => 94109400
enum = Enumerator.new(3) do |yielder| ...; end
def square_times(num)
return to_enum(:square_times) {num ** 2} unless block_given?
(num ** 2).times do |i|
yield i
end
end
module Palindrome
refine String do
def palindrome?
self == self.reverse
end
end
end
using Palindrome # Monkey patch now active for context
"saippuakivikauppias".palindrome? # => true
%i[foo bar baz] # => [:foo, :bar, :baz]
%I[#{1 + 1}x #{2 + 2}x] #=> [:"2x", :"4x"]
haystack = (1..99999999)
haystack.bsearch do |needle|
needle == 12345
end
# => 12345
#!/usr/bin/env ruby1.9
#encoding: utf-8
puts "★"
#!/usr/bin/env ruby-2.0
puts "★"
# Ruby 1.8
require File.dirname(__FILE__) + "/lib"
File.read(File.dirname(__FILE__) + "/.config")
# Ruby 1.9
require_relative 'lib'
File.read(File.dirname(__FILE__) + '/.config')
# Ruby 2.0
require_relative 'lib'
File.read(__dir__ + '/.config')
Car = Struct.new(:make, :model, :year) do
def build
#...
end
end
car = Car.new('Toyota', 'Prius', 2014)
car.to_h # => {:make=>"Toyota", :model=>"Prius", :year=>2014}
nil.to_h # => {}
Implemented for: