Skip to content

Commit

Permalink
Add console presentation on ruby 2.4 new features
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Jan 18, 2017
1 parent e9f18f5 commit 047d8a8
Show file tree
Hide file tree
Showing 75 changed files with 407 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/23/01.rb
@@ -0,0 +1,3 @@
p !!/foo/.match("foo")
p !!"foo".match(/foo/)
p !!:foo.match(/foo/)
10 changes: 10 additions & 0 deletions 2017-01-ruby-2.4/23/02.rb
@@ -0,0 +1,10 @@
Thread.new do
begin
raise 'foo'
rescue Exception => e
puts "#{Thread.current} terminated with exception:"
puts "#{e.backtrace.first}: #{e.message} (#{e.class})"
end
end
sleep 0.01

5 changes: 5 additions & 0 deletions 2017-01-ruby-2.4/23/03.rb
@@ -0,0 +1,5 @@
require 'irb'
IRB.setup(eval("__FILE__"))
irb = IRB::Irb.new(IRB::WorkSpace.new(binding))
IRB.conf[:MAIN_CONTEXT] = irb.context
irb.eval_input
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/23/04.rb
@@ -0,0 +1,2 @@
p Integer.equal?(Integer)
p Integer.equal?(Integer)
7 changes: 7 additions & 0 deletions 2017-01-ruby-2.4/23/05.rb
@@ -0,0 +1,7 @@
# No equivalent
s = "\u00EB\u00C4\u00D6"
p s
p s.upcase
p s.downcase
p s.swapcase
p s.capitalize
8 changes: 8 additions & 0 deletions 2017-01-ruby-2.4/23/06.rb
@@ -0,0 +1,8 @@
if (a, b = [1, 2]; [1,2])
p a
p b
end
unless (a, b = nil; nil)
p a
p b
end
13 changes: 13 additions & 0 deletions 2017-01-ruby-2.4/23/07.rb
@@ -0,0 +1,13 @@
module Foo
refine String do
def a; 1; end
end
end

using Foo
meth = :a
eval <<END
using Foo
p 'a'.#{meth}
p 'ab'.each_char.map{|s| s.#{meth}}
END
6 changes: 6 additions & 0 deletions 2017-01-ruby-2.4/23/08.rb
@@ -0,0 +1,6 @@
puts ((raise rescue 1))
begin
eval('puts(raise rescue 1)')
rescue SyntaxError
puts "Still Bad"
end
4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/23/09.rb
@@ -0,0 +1,4 @@
p 1
if false
p 2
end
22 changes: 22 additions & 0 deletions 2017-01-ruby-2.4/23/10.rb
@@ -0,0 +1,22 @@
# No equivalent
o = Object.new
def o.foo; 1 end
o.freeze

begin
x = o.clone
def x.bar; foo + 2 end
p x.bar
rescue
p $!
end

begin
x = o.dup
def x.bar; foo + 2 end
x.freeze
p x.bar
rescue
p $!
end

4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/23/11.rb
@@ -0,0 +1,4 @@
# No equivalent
$VERBOSE = true
instance_variable_get(:@foo)
$a
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/23/12.rb
@@ -0,0 +1,2 @@
p (1...100).inject(0, :+)
p (1...100).to_a.inject(0, :+)
7 changes: 7 additions & 0 deletions 2017-01-ruby-2.4/23/13.rb
@@ -0,0 +1,7 @@
s = StringIO.new <<END
a
b
a
c
END
p s.each_line.to_a.uniq
4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/23/14.rb
@@ -0,0 +1,4 @@
p File.zero?(__FILE__)
p Dir.new(__dir__).entries.length == 2
require 'pathname'
p File.zero?(Pathname.new(__FILE__).to_s)
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/23/15.rb
@@ -0,0 +1,3 @@
m = 'food'.match(/(?<a>.)(?<b>..)(?<c>.)/)
p m.names.inject({}){|a, n| a[n] = m[n]; a}
p [:a, :b, :c].map{|c| m[c]}
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/23/16.rb
@@ -0,0 +1 @@
p 1234.to_s.split(//).reverse.map(&:to_i)
5 changes: 5 additions & 0 deletions 2017-01-ruby-2.4/23/17.rb
@@ -0,0 +1,5 @@
f = 12.35678
p((f*(10**1)).ceil/(10.0**1))
p((f*(10**2)).floor/(10.0**2))
p((f*(10**3)).round/(10.0**3))
p((f*(10**4)).truncate/(10.0**4))
4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/23/18.rb
@@ -0,0 +1,4 @@
f = 12.5
p ((f.round - f == 0.5) ? (f.round.odd? ? f.round - 1 : f.round) : f.round)
p f.round
p ((f.round - f == 0.5) ? f.round - 1 : f.round)
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/23/19.rb
@@ -0,0 +1 @@
p :foo.to_s.match(/foo/)
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/23/20.rb
@@ -0,0 +1,3 @@
p [3].concat([1]).concat([2])
p "foo".concat("bar").concat("baz")
p "foo".prepend("baz").prepend("bar")
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/23/21.rb
@@ -0,0 +1,3 @@
p 1 < 3 ? 3 : (1 > 5 ? 5 : 1)
p 4 < 3 ? 3 : (4 > 5 ? 5 : 4)
p 7 < 3 ? 3 : (7 > 5 ? 5 : 7)
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/23/22.rb
@@ -0,0 +1 @@
p({a: 1, b: nil}.inject({}){|a, (k,v)| a[k] = v unless v.nil?; a})
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/23/23.rb
@@ -0,0 +1 @@
p({a: 1, b: nil}.inject({}){|a, (k,v)| a[k] = !v; a})
4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/23/24.rb
@@ -0,0 +1,4 @@
File.open('runner.rb').each_line{|l| p l.chomp; break}
StringIO.new("a\nb\n").each_line{|l| p l.chomp}
"a\nb\n".each_line{|l| p l.chomp}
p "a\nb\n".lines.map(&:chomp)
9 changes: 9 additions & 0 deletions 2017-01-ruby-2.4/23/25.rb
@@ -0,0 +1,9 @@
# No equivalent
module Foo
refine Enumerable do
def a; 1 end
end
end

using Foo
p [].a
10 changes: 10 additions & 0 deletions 2017-01-ruby-2.4/23/26.rb
@@ -0,0 +1,10 @@
# No equivalent
module Foo
refine String do
def a; 1 end
end
end

p Module.used_modules
using Foo
p Module.used_modules
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/23/27.rb
@@ -0,0 +1 @@
p "\x10\x00".unpack('s').first
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/23/28.rb
@@ -0,0 +1,3 @@
s = ''.b
s.replace([1].pack('s'))
p s
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/23/29.rb
@@ -0,0 +1,2 @@
p ((x = 'foo'.casecmp('Foo')).is_a?(Integer) ? x.zero? : x)
p ((x = :foo.casecmp(:Bar)).is_a?(Integer) ? x.zero? : x)
6 changes: 6 additions & 0 deletions 2017-01-ruby-2.4/23/30.rb
@@ -0,0 +1,6 @@
require 'net/http'
require 'uri'
url = URI('http://localhost/')
Net::HTTP.start(url.hostname, url.port) do |http|
http.post(url.path, "q" => "ruby")
end
4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/23/31.rb
@@ -0,0 +1,4 @@
# No built-in equivalent
require 'double_bag_ftps'
ftp = DoubleBagFTPS.new
ftp.connect('localhost')
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/23/32.rb
@@ -0,0 +1,3 @@
p TRUE
p FALSE
p NIL
5 changes: 5 additions & 0 deletions 2017-01-ruby-2.4/23/33.rb
@@ -0,0 +1,5 @@
require 'logger'
logger = Logger.new($stderr)
logger.level = :info
logger.progname = 'L2'
logger.info 'foo'
14 changes: 14 additions & 0 deletions 2017-01-ruby-2.4/23/34.rb
@@ -0,0 +1,14 @@
require 'optparse'
h = {}
OptionParser.new do |o|
o.on "-f", "--foo=HOST" do |v|
h[:foo] = v
end
o.on "-b", "--bar=PORT", Integer do |v|
h[:bar] = v
end
o.on "-B", "--baz" do
h[:baz] = true
end
end.parse(%w'-f FOO --bar 3 -B')
p h
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/23/35.rb
@@ -0,0 +1,3 @@
p (10.0**400) < Float::MIN ? -1 : ((10.0**400) > Float::MAX ? 1: true)
p -(10.0**400) < Float::MIN ? -1 : (-(10.0**400) > Float::MAX ? 1: true)
p 1.0 < Float::MIN ? -1 : (1.0 > Float::MAX ? 1: true)
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/23/36.rb
@@ -0,0 +1,2 @@
# No equivalent
p String.new
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/01.rb
@@ -0,0 +1,3 @@
p /foo/.match?("foo")
p "foo".match?(/foo/)
p :foo.match?(/foo/)
5 changes: 5 additions & 0 deletions 2017-01-ruby-2.4/24/02.rb
@@ -0,0 +1,5 @@
Thread.new do
Thread.current.report_on_exception = true
raise 'foo'
end
sleep 0.01
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/24/03.rb
@@ -0,0 +1,2 @@
require 'irb'
binding.irb
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/24/04.rb
@@ -0,0 +1,2 @@
p Fixnum.equal?(Integer)
p Bignum.equal?(Integer)
6 changes: 6 additions & 0 deletions 2017-01-ruby-2.4/24/05.rb
@@ -0,0 +1,6 @@
s = "\u00EB\u00C4\u00D6"
p s
p s.upcase
p s.downcase
p s.swapcase
p s.capitalize
8 changes: 8 additions & 0 deletions 2017-01-ruby-2.4/24/06.rb
@@ -0,0 +1,8 @@
if (a, b = [1, 2])
p a
p b
end
unless (a, b = nil)
p a
p b
end
10 changes: 10 additions & 0 deletions 2017-01-ruby-2.4/24/07.rb
@@ -0,0 +1,10 @@
module Foo
refine String do
def a; 1; end
end
end

using Foo
meth = :a
p 'a'.send(meth)
p 'ab'.each_char.map(&meth)
6 changes: 6 additions & 0 deletions 2017-01-ruby-2.4/24/08.rb
@@ -0,0 +1,6 @@
puts (raise rescue 1)
begin
eval('puts(raise rescue 1)')
rescue SyntaxError
puts "Still Bad"
end
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/09.rb
@@ -0,0 +1,3 @@
p 1
return
p 2
7 changes: 7 additions & 0 deletions 2017-01-ruby-2.4/24/10.rb
@@ -0,0 +1,7 @@
o = Object.new
def o.foo; 1 end
o.freeze
x = o.clone(freeze: false)
def x.bar; foo + 2 end
x.freeze
p x.bar
6 changes: 6 additions & 0 deletions 2017-01-ruby-2.4/24/11.rb
@@ -0,0 +1,6 @@
$VERBOSE = true
def Warning.warn(s)
puts "WARNING: #{s}" unless s =~ /instance variable @.+ not initialized/
end
instance_variable_get(:@foo)
$a
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/24/12.rb
@@ -0,0 +1,2 @@
p (1...100).sum
p (1...100).to_a.sum
7 changes: 7 additions & 0 deletions 2017-01-ruby-2.4/24/13.rb
@@ -0,0 +1,7 @@
s = StringIO.new <<END
a
b
a
c
END
p s.each_line.uniq
4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/24/14.rb
@@ -0,0 +1,4 @@
p File.empty?(__FILE__)
p Dir.empty?(__dir__)
require 'pathname'
p Pathname.new(__FILE__).empty?
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/15.rb
@@ -0,0 +1,3 @@
m = 'food'.match(/(?<a>.)(?<b>..)(?<c>.)/)
p m.named_captures
p m.values_at(:a, :b, :c)
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/24/16.rb
@@ -0,0 +1 @@
p 1234.digits
5 changes: 5 additions & 0 deletions 2017-01-ruby-2.4/24/17.rb
@@ -0,0 +1,5 @@
f = 12.35678
p f.ceil(1)
p f.floor(2)
p f.round(3)
p f.truncate(4)
4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/24/18.rb
@@ -0,0 +1,4 @@
f = 12.5
p f.round(half: :even)
p f.round(half: :up)
p f.round(half: :down)
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/24/19.rb
@@ -0,0 +1 @@
p :foo.match(/foo/)
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/20.rb
@@ -0,0 +1,3 @@
p [3].concat([1], [2])
p "foo".concat("bar", "baz")
p "foo".prepend("bar", "baz")
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/21.rb
@@ -0,0 +1,3 @@
p 1.clamp(3, 5)
p 4.clamp(3, 5)
p 7.clamp(3, 5)
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/24/22.rb
@@ -0,0 +1 @@
p({a: 1, b: nil}.compact)
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/24/23.rb
@@ -0,0 +1 @@
p({a: 1, b: nil}.transform_values{|v| !v})
4 changes: 4 additions & 0 deletions 2017-01-ruby-2.4/24/24.rb
@@ -0,0 +1,4 @@
File.open('runner.rb').each_line(chomp: true){|l| p l; break}
StringIO.new("a\nb\n").each_line(chomp: true){|l| p l}
"a\nb\n".each_line(chomp: true){|l| p l}
p "a\nb\n".lines(chomp: true)
8 changes: 8 additions & 0 deletions 2017-01-ruby-2.4/24/25.rb
@@ -0,0 +1,8 @@
module Foo
refine Enumerable do
def a; 1 end
end
end

using Foo
p [].a
9 changes: 9 additions & 0 deletions 2017-01-ruby-2.4/24/26.rb
@@ -0,0 +1,9 @@
module Foo
refine String do
def a; 1 end
end
end

p Module.used_modules
using Foo
p Module.used_modules
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/24/27.rb
@@ -0,0 +1 @@
p "\x10\x00".unpack1('s')
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/28.rb
@@ -0,0 +1,3 @@
s = ''.b
[1].pack('s', :buffer=>s)
p s
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/24/29.rb
@@ -0,0 +1,2 @@
p 'foo'.casecmp?('Foo')
p :foo.casecmp?(:Bar)
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/30.rb
@@ -0,0 +1,3 @@
require 'net/http'
require 'uri'
Net::HTTP.post(URI('http://localhost/'), {"q" => "ruby"})
2 changes: 2 additions & 0 deletions 2017-01-ruby-2.4/24/31.rb
@@ -0,0 +1,2 @@
require 'net/ftp'
ftp = Net::FTP.new('localhost', :ssl=>true)
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/32.rb
@@ -0,0 +1,3 @@
p TRUE
p FALSE
p NIL
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/33.rb
@@ -0,0 +1,3 @@
require 'logger'
logger = Logger.new($stderr, level: :info, progname: "L2")
logger.info 'foo'
8 changes: 8 additions & 0 deletions 2017-01-ruby-2.4/24/34.rb
@@ -0,0 +1,8 @@
require 'optparse'
h = {}
OptionParser.new do |o|
o.on "-f", "--foo=HOST"
o.on "-b", "--bar=PORT", Integer
o.on "-B", "--baz"
end.parse(%w'-f FOO --bar 3 -B', :into=>h)
p h
3 changes: 3 additions & 0 deletions 2017-01-ruby-2.4/24/35.rb
@@ -0,0 +1,3 @@
p (10.0**400).infinite?
p (-(10.0**400)).infinite?
p (1.0).finite?
1 change: 1 addition & 0 deletions 2017-01-ruby-2.4/24/36.rb
@@ -0,0 +1 @@
p String.new(:capacity=>1 << 30)

0 comments on commit 047d8a8

Please sign in to comment.