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
begin | |
# prompt> gem install enumerable_lz | |
require 'enumerable_lz' | |
rescue LoadError | |
# prompt> gem install enumerable-lazy if ruby --version < '2.0.0' | |
require 'enumerable/lazy' if RUBY_VERSION < '2.0' | |
module Enumerable | |
def transform █lazy.map(&block);end | |
end | |
end |
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
array = [1, 2, 3, 4] | |
# So... | |
Hash[*array].map { |k, v| "#{k} - #{v}<br>" }.join | |
# versus | |
array.each_slice(2).map { |s| s.join(' - ') }.join '<br>' |
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 ditch_line ditched_line | |
self.split("\n").delete_if { |line| line == ditched_line }.join("\n") | |
end | |
end | |
"Hello there\nGoodbye now\nCome again".ditch_line 'Hello there' | |
#=> "Goodbye now\nCome again" |
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
# -*- coding: utf-8 -*- | |
class WikiTable | |
attr_accessor :rows, :attributes | |
class Row | |
attr_accessor :cells, :attributes | |
def initialize | |
@cells = [] | |
end | |
def <<(c); cells << c ; end |
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 Dictionary | |
attr_reader :entries | |
def initialize | |
@entries = {} | |
end | |
def add naked_hash | |
naked_hash.each do |k, v| | |
@entries[k] = v |
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
#!/usr/bin/ruby | |
require 'socket' | |
require 'thread' | |
require 'optparse' | |
require 'tmpdir' | |
class ProxyServer | |
attr_reader :switches | |
attr_reader :listen_port, :remote_host, :remote_port |
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
p [:ruby, :"2.0", :symbol, :array, :literal] | |
#=> [:ruby, :"2.0", :symbol, :array, :literal] | |
p %i(ruby 2.0 symbol array literal) | |
#=> [:ruby, :"2.0", :symbol, :array, :literal] |
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
#!/usr/bin/ruby | |
require 'enumerable/lazy' if RUBY_VERSION<'2.0' | |
a,b=2,1;p (0..1/0.0).lazy.map{a,b=a*2+b*3,a+b*2;s=2*(a%3==1?a+1:a-1)}.take_while{|s|s<=10**9}.reduce(:+) |
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
# run using ```rvm jruby-1.6.7 do jruby "-J-Xmx2000m" "--1.9" tej.rb``` | |
require 'rubygems' | |
require 'nokogiri' | |
require 'csv' | |
f = File.open("/tmp/preview.html") | |
doc = Nokogiri::HTML(f) | |
csv = CSV.open("/tmp/output.csv", 'w',{:col_sep => ",", :quote_char => '\'', :force_quotes => true}) |
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
fizz = [nil, nil, "Fizz"].cycle | |
buzz = [nil, nil, nil, nil, "Buzz"].cycle | |
(1..100).zip(fizz, buzz) do |num, *fb| | |
puts fb.compact.reduce(:+) || num | |
end |