TbpgrUtils

Build Status Coverage Status Code Climate

TbpgrUtils is Utilities.

Installation

Add this line to your application's Gemfile:

gem 'tbpgr_utils'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tbpgr_utils

Usage

List

class/module/method mean
TbpgrUtils Array#together loop all arrays by block
TbpgrUtils Array#together_at together version of Array#at. together_at has alias :tat
TbpgrUtils Array#together_clear together version of Array#clear. together_clear has alias :tclear
TbpgrUtils Array#together_compact together version of Array#compact. together_compact has alias :tcompact. this is immutable.
TbpgrUtils Array#together_compact! together version of Array#compact!. together_compact! has alias :tcompact! this is mutable.
TbpgrUtils Array#together_concat together version of Array#concat. together_concat has alias :tconcat
TbpgrUtils Array#together_delete together version of Array#delete. together_delete has alias :tdelete
TbpgrUtils Array#together_delete_at together version of Array#delete_at. together_delete_at has alias :tdelete_at
TbpgrUtils Array#together_delete_if together version of Array#delete_if. together_delete_if has alias :tdelete_if
TbpgrUtils Array#together_empty? together version of Array#empty?. together_empty? has alias :tempty?
TbpgrUtils Array#together_fill together version of Array#fill. together_fill has alias :tfill
TbpgrUtils Array#together_first together version of Array#first. together_first has alias :tfirst
TbpgrUtils Array#together_include? together version of Array#include?. together_include? has alias :tinclude?
TbpgrUtils Array#together_index together version of Array#index. together_index has alias :tindex
TbpgrUtils Array#together_insert together version of Array#insert. together_insert has alias :tinsert
TbpgrUtils Array#together_last together version of Array#last. together_last has alias :tlast
TbpgrUtils Array#together_map together version of Enumerable#map. together_map has aliases [:tmap, :together_collect, :tcollect]
TbpgrUtils Array#together_map! together version of Enumerable#map!. together_map! has aliases [:tmap!, :together_collect!, :tcollect!]
TbpgrUtils Array#together_pop together version of Array#pop. together_pop has alias :tpop
TbpgrUtils Array#together_reduce together version of Enumerable#reduce. together_reduce has aliases [:treduce, :together_inject, :tinject]
TbpgrUtils Array#together_reverse together version of Array#reverse. together_reverse has alias :treverse
TbpgrUtils Array#together_reverse! together version of Array#reverse!. together_reverse! has alias :treverse!
TbpgrUtils Array#together_sample together version of Array#sample. together_sample has alias :tsample
TbpgrUtils Array#together_select together version of Enumerable#select. together_select has aliases [:tselect, :together_find_all, :tfindall]
TbpgrUtils Array#together_shift together version of Array#shift. together_shift has alias :tshift
TbpgrUtils Array#together_shuffle together version of Array#shuffle. together_shuffle has alias :tshuffle
TbpgrUtils Array#together_slice together version of Array#slice. together_slice has alias :tslice
TbpgrUtils Array#together_with_index loop all arrays by block with index
AttributesHashable.to_hash define to_hash method for get instance_values
AttributesInitializable::ClassMethods.attr_accessor_init generate attr_accessor + initializer
AttributesInitializable::ClassMethods.attr_reader_init generate attr_reader + initializer
AttributesInitializable::ClassMethods.attr_writer init generate attr_writer + initializer
EndERB.apply for single template script using END and DATA
EvalHelper Object enable to use EvalHelper in Object
EvalHelper#each_do_code create each do code, for eval
EvalHelper#each_brace_code create each brace single line code, for eval
EvalHelper#each_with_index_brace_code create eachwith_index_ brace single line code, for eval
EvalHelper#if_code create if strings, for eval
EvalHelper#if_code_after create after-if strings, for eval
EvalHelper#require_code create require strings, for eval
EvalHelper#require_relative_code create require_relative strings, for eval
EvalHelper#set_variable_code create set_variable_code strings, for eval
EvalHelper#set_variables_code create set_variables_code strings, for eval
EvalHelper#times_code create times_code strings, for eval
EvalHelper#ternary_operator create ternary operator strings, for eval
EvalHelper#unless_code create unless strings, for eval
EvalHelper#unless_code_after create after-unless strings, for eval
TbpgrUtils File.insert_bom insert BOM to UTF-8 File
Ghostable module help to create ghost method(dynamic method define by ussing method_missing + pattern-method-name)
TbpgrUtils Kernel#bulk_define_methods define methods to classes. methods have simple return value.
TestToolbox Kernel#capture_stdout capture STDOUT
TestToolbox Kernel#dp_line debug print line for print-debugging
TbpgrUtils Kernel#aa_ancestors Ascii Art Ancestors
TbpgrUtils Kernel#print_eval Print code + eval result
TbpgrUtils Kernel#puts_eval Puts code + eval result
TbpgrUtils Kernel#bulk_puts_eval Puts each-line-code + eval result
MetasyntacticVariable META variable, META variable for classes
TbpgrUtils Module.alias_methods create alias methods
TbpgrUtils Object#any_of? if self match any one of items, return true
TbpgrUtils Object#boolean? data type check for boolean
TbpgrUtils Object#guard data type check for guard
TbpgrUtils Object#unless_guard data type check for unless_guard
TbpgrUtils Object#my_methods return public/protected/private self define methods
TbpgrUtils Object#to_bool syntax sugar of !!. convert [false, nil] => fasel, other => true.
TbpgrUtils String#comma_to_a comma-format string to array
TbpgrUtils String#hyphen_to_a hyphen-format string to array
TbpgrUtils String#justify_table justify pipe format table string
TbpgrUtils String#say say string
TbpgrUtils String#stripe stripe string
TbpgrUtils String#surround surround string
TbpgrUtils String#to_hatena_heading create hatena-format heading string with Emmet-like grammar
TbpgrUtils String#to_markdown_heading create markdown-format heading string with Emmet-like grammar
TbpgrUtils String#to_space2_heading create space2-format heading string with Emmet-like grammar
TbpgrUtils String#to_space4_heading create space4-format heading string with Emmet-like grammar
TbpgrUtils String#to_tab_heading create tab-format heading string with Emmet-like grammar
Templatable module get result from template + placeholder
TemplateMethodable module for Template Method Pattern

Array#together

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 4 3}
[alpha, numbers].together do |first, second|
  print "#{first}:#{second}\n"  # => output one:1, two:2, three:3
end

back to list

Array#together_at

require 'tbpgr_utils'

# same elements size case
alpha = %w{one two three}
numbers = %w{1 2 3}
print [alpha, numbers].together_at 2 # => output ['three', 3]

# different elements size case
alpha = %w{one two three}
numbers = %w{1 2}
print [alpha, numbers].together_at 2 # => output ['three', nil]

back to list

Array#together_clear

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together_clear # => [[], []]

back to list

Array#together_compact

require 'tbpgr_utils'

alpha = ['a','b','c', nil,'d']
numbers = [1, 2, nil, 3]
lists = [alpha, numbers]
ret = lists.together_compact
print lists # => output [['a','b','c', nil,'d'], [1, 2, nil, 3]]
print ret # => output [['a','b','c','d'], [1, 2, 3]]

back to list

Array#together_compact!

require 'tbpgr_utils'

alpha = ['a','b','c', nil,'d']
numbers = [1, 2, nil, 3]
lists = [alpha, numbers]
ret = lists.together_compact!
print lists # => output [['a','b','c','d'], [1, 2, 3]]
print ret # => output [['a','b','c','d'], [1, 2, 3]]

back to list

Array#together_concat

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together_concat [4, 5, 6]

print alpha # => ["one", "two", "three", 4, 5, 6]
print numbers # => ["1", "2", "3", 4, 5, 6]

back to list

Array#together_delete

require 'tbpgr_utils'

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete 2
print lists # => output [[1, 3, 4], [3, 4, 5]]

if delete target is not exist

require 'tbpgr_utils'

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete 6
print ret # => nil
print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]

if delete target is not exist and use block

require 'tbpgr_utils'

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete(6) { 999 }
print ret # => 999
print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]

back to list

Array#together_delete_at

if delete_at target is exist

require 'tbpgr_utils'

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete_at 2
print ret # => [3, 4]
print lists # => output [[1, 2, 4], [2, 3, 5]]

if delete_at target is not exist

require 'tbpgr_utils'

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete_at 6
print ret # => [nil, nil]
print lists # => output [[1, 2, 3, 4], [2, 3, 4, 5]]

if delete_at target is exist(minus index)

require 'tbpgr_utils'

child1 = [1, 2, 3, 4]
child2 = [2, 3, 4, 5]
lists = [child1, child2]
ret = lists.together_delete_at -3
print ret # => [2, 3]
print lists # => output [[1, 3, 4], [2, 4, 5]]

back to list

Array#together_delete_if

if delete_if target is exist

require 'tbpgr_utils'

lists = [[1, 2, 3, 4], [6, 4, 6, 8]]
ret = lists.together_delete_if {|first, second|(first + second).odd?}
print ret # => [[2, 4], [4, 8]]

if delete_if target is not exist. return nil.

require 'tbpgr_utils'

lists = [[2, 2, 4, 4], [6, 4, 6, 8]]
ret = lists.together_delete_if {|first, second|(first + second).odd?}
print ret # => nil

back to list

Array#together_empty?

empty case

require 'tbpgr_utils'

lists = [[], []]
ret = lists.together_empty?
print ret # => true

not empty case

require 'tbpgr_utils'

lists = [[1], []]
ret = lists.together_empty?
print ret # => false

back to list

Array#together_fill

not use block case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_fill(99)
print ret # => [[99, 99, 99, 99, 99], [99, 99, 99, 99, 99]]

use block, no args case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_fill { |i|(i + 1) + 1 }
print ret # => [[2, 3, 4, 5, 6], [2, 3, 4, 5, 6]]

use block, has args case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_fill(2) { |i|(i + 1) + 1 }
print ret # => [[1, 2, 4, 5, 6], [6, 7, 4, 5, 6]]

back to list

Array#together_first

no args case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_first
print ret # => [1, 6]

has args 2 case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_first 2
print ret # => [[1, 2], [6, 7]]

has args 0 case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_first 0
print ret # => [[], []]

has args over size case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_first 6
print ret # => [[*1..5], [*6..10]]

back to list

Array#together_include?

together_include? is bulk version of Array#include?

together_include? has alias :tinclude?

both include single ret case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 5
print ret # => true

one include single ret case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 9
print ret # => true

both not include single ret case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 10
print ret # => false

both include multi ret case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 5, true
print ret # => [true, true]

one include multi ret case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 9, true
print ret # => [false, true]

both not include multi ret case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_include? 10, true
print ret # => [false, false]

back to list

Array#together_index

together_index has alias :tindex

both index exist case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_index 5
print ret # => [4, 0]

one include single ret case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_index 4
print ret # => [3, nil]

both not include single ret case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_index 10
print ret # => [nil, nil]

back to list

Array#together_insert

together_insert has alias :tinsert

both insert exist case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_insert(1, 55, 66)
print ret # => [[1, 55, 66, 2, 3, 4, 5], [5, 55, 66, 6, 7, 8, 9]]

both insert exist and minus index case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_insert(-2, 55, 66)
print ret # => [[1, 2, 3, 4, 55, 66, 5], [5, 6, 7, 8, 55, 66, 9]]

both insert exist case

require 'tbpgr_utils'

lists = [[*1..5], [*5..9]]
ret = lists.together_insert(6, 55, 66)
print ret # => [[1, 2, 3, 4, 5, nil, 55, 66], [5, 6, 7, 8, 9, nil, 55, 66]],

back to list

Array#together_last

together_last has alias :tlast

no args case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_last
print ret # => [5, 10]

has args 2 case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_last 2
print ret # => [[4, 5], [9, 10]]

has args 0 case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_last 0
print ret # => [[], []]

has args over size case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_last 6
print ret # => [[*1..5], [*6..10]]

back to list

Array#together_map(or tmap, together_collect, tcollect)

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 2 3}
ret = [alpha, numbers].together_map {|first, second|"#{first}:#{second}"}
print ret # => output [one:1, two:2, three:3]

if you want to return multi array, following.

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 2 3}
ret = [alpha, numbers].together_map {|first, second|[["#{first}:ret"], ["#{second}:ret"]]}
print ret # => output [["one:ret", "two:ret", "three:ret"],["1:ret", "2:ret", "3:ret"]]

back to list

Array#together_map!(or tmap!, together_collect!, tcollect!)

if you want to return single array, following.

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 2 3}
ary = [alpha, numbers]
ret = ary.together_map! do |first, second|
  "#{first}:#{second}"
end
print ret # => output ['one:1', 'two:2', 'three:3']
print ary # => output ['one:1', 'two:2', 'three:3']

if you want to return multi array, following.

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 2 3}
ary = [alpha, numbers]
ret = ary.together_map! do |first, second|
  ["#{first}:#{second}", "#{second}:#{first}"]
end
print ret # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]
print ary # => output [['1:one', '2:two', '3:three'], ['one:1', 'two:2', 'three:3']]

back to list

Array#together_pop(or tpop)

together_pop has alias :tpop

not empty case

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_pop
print ret # => [2, 6]
print lists # => [1, 5]

empty case

require 'tbpgr_utils'

lists = [[], []]
ret = lists.together_pop
print ret # => [nil, nil]
print lists # => [[], []]

not empty case with args

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_pop 2
print ret # => [[1, 2], [5, 6]]
print lists # => [[], []]

not empty case with args

require 'tbpgr_utils'

lists = [[], []]
ret = lists.together_pop 2
print ret # => [[], []]
print lists # => [[], []]

back to list

Array#together_reduce(or :treduce, :together_inject, :tinject)

  • if you want to single return
require 'tbpgr_utils'

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_reduce{|memo, first, second|memo + first + second}
print ret # => output  20
  • if you want to single return with init value
require 'tbpgr_utils'

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_reduce(10){|memo, first, second|memo + first + second}
print ret # => output  30
  • if you want to single return with init string value
require 'tbpgr_utils'

firsts = %w{a b c}
seconds =  %w{1 2 3}
ret = [firsts, seconds].together_reduce('start-'){|memo, first, second|memo + first + second}
print ret # => output 'start-a1b2c3'
  • if you want to single return with init Array value
require 'tbpgr_utils'

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_reduce([]){|memo, first, second|memo << first + second}
print ret # => output [5, 4, 6, 5]
  • if you want to single return with init Hash value
require 'tbpgr_utils'

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_reduce({}){|memo, first, second|memo[first] = second;memo}
print ret # => output {1=>4, 2=>2, 3=>3, 4=>1}

back to list

Array#together_reverse(or :treverse)

together_reverse has alias :treverse

not empty case

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_reverse
print ret # => [[2, 1], [6, 5]]
print lists # => [[1, 2], [5, 6]]

one empty case

require 'tbpgr_utils'

lists = [[1, 2], []]
ret = lists.together_reverse
print ret # => [[2, 1], []]
print lists # => [[1, 2], []]

back to list

Array#together_reverse!(or :treverse!)

together_reverse! has alias :treverse!

not empty case

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_reverse!
print ret # => [[2, 1], [6, 5]]
print lists # => [[2, 1], [6, 5]]

one empty case

require 'tbpgr_utils'

lists = [[1, 2], []]
ret = lists.together_reverse!
print ret # => [[2, 1], []]
print lists # => [[2, 1], []]

back to list

Array#together_sample(or :tsample)

together_sample has alias :tsample

not empty case

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_sample
print ret # => [1 or 2, 5 or 6]

empty case

require 'tbpgr_utils'

lists = [[], []]
ret = lists.together_sample
print ret # => [nil, nil]

not empty case with args

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_sample 2
print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]] 

not empty case with args

require 'tbpgr_utils'

lists = [[], []]
ret = lists.together_sample 2
print ret # => [[], []]

not empty, over size case with args

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_sample 3
print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]] 

back to list

Array#together_select(or tselect, together_find_all, tfindall)

require 'tbpgr_utils'

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_select{|first, second|first == second}
print ret # => output  [[2, 3], [2, 3]]

if you want to return multi array, following.

require 'tbpgr_utils'

firsts = [1, 2, 3, 4]
seconds =  [4, 2, 3, 1]
ret = [firsts, seconds].together_select{|first, second|[first.odd?, second.even?]}
print ret # => output  [[1, 3], [4, 2]]

back to list

Array#together_shift(or tshift)

together_shift has alias :tshift

not empty case

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_shift
print ret # => [1, 5]
print lists # => [2, 6]

empty case

require 'tbpgr_utils'

lists = [[], []]
ret = lists.together_shift
print ret # => [nil, nil]
print lists # => [[], []]

not empty case

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_shift 2
print ret # => [[1, 2], [5, 6]]
print lists # => [[], []]

not empty case

require 'tbpgr_utils'

lists = [[], []]
ret = lists.together_shift 2
print ret # => [[], []]
print lists # => [[], []]

back to list

Array#together_shuffle(or :tshuffle)

together_shuffle has alias :tshuffle

require 'tbpgr_utils'

lists = [[1, 2], [5, 6]]
ret = lists.together_shuffle
print ret # => [[1 or 2, 1 or 2], [5 or 6, 5 or 6]]

back to list

Array#together_slice(or :tslice)

single args case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_slice 2
print ret # => [3, 8]

multi args case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_slice 2, 2
print ret # => [[3, 4], [8, 9]]

range args case

require 'tbpgr_utils'

lists = [[*1..5], [*6..10]]
ret = lists.together_slice (2..3)
print ret # => [[3, 4], [8, 9]]

back to list

Array#together_with_index

require 'tbpgr_utils'

alpha = %w{one two three}
numbers = %w{1 2 3}
[alpha, numbers].together_with_index do |first, second, index|
  print "#{index.to_s}:#{first}:#{second}\n"  # => output 0:one:1, 1:two:2, 2:three:3
end

back to list

AttributesHashable.to_hash

require 'attributes_initializable'
require 'attributes_hashable'

class Hoge
  include AttributesInitializable
  attr_accessor_init :hoge, :hige
  include AttributesHashable
end

hoge = Hoge.new do |h|
  h.hoge = 'hoge'
  h.hige = 'hige'
end

hoge.to_hash # => {:hoge=>"hoge", :hige=>"hige"}

# After include AttributesHashable, you can use Hash.try_convert.
Hash.try_convert hoge # => {:hoge=>"hoge", :hige=>"hige"}

back to list

AttributesInitializable::ClassMethods.attr_accessor_init

require 'attributes_initializable'

class AccessorSample
  include AttributesInitializable
  attr_accessor_init :atr1, :atr2
end

atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
p atr_sample1.atr1 # => atr1
p atr_sample1.atr2 # => atr2

atr_sample2 = AccessorSample.new do |a|
  a.atr1 = 'atr1'
  a.atr2 = 'atr2'
end
p atr_sample2.atr1 # => atr1
p atr_sample2.atr2 # => atr2

same mean code is

class AccessorSample
  attr_accessor :atr1, :atr2

  def initialize(values = nil, &block)
    return yield self if block
    @atr1 = values[:atr1]
    @atr2 = values[:atr2]
  end
end

atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
p atr_sample1.atr1 # => atr1
p atr_sample1.atr2 # => atr2

atr_sample2 = AccessorSample.new do |a|
  a.atr1 = 'atr1'
  a.atr2 = 'atr2'
end
p atr_sample2.atr1 # => atr1
p atr_sample2.atr2 # => atr2

back to list

AttributesInitializable::ClassMethods.attr_reader_init

require 'attributes_initializable'

class AccessorSample
  include AttributesInitializable
  attr_reader_init :atr1, :atr2
end

atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
p atr_sample1.atr1 # => atr1
p atr_sample1.atr2 # => atr2

# can not use writer.
# atr_sample2 = AccessorSample.new do |a|
#   a.atr1 = 'atr1'
#   a.atr2 = 'atr2'
# end

same mean code is

class AccessorSample
  attr_reader :atr1, :atr2

  def initialize(values = nil, &block)
    return yield self if block
    @atr1 = values[:atr1]
    @atr2 = values[:atr2]
  end
end

atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
p atr_sample1.atr1 # => atr1
p atr_sample1.atr2 # => atr2

# can not use writer.
# atr_sample2 = AccessorSample.new do |a|
#   a.atr1 = 'atr1'
#   a.atr2 = 'atr2'
# end

back to list

AttributesInitializable::ClassMethods.attr_writer_init

require 'attributes_initializable'

class AccessorSample
  include AttributesInitializable
  attr_writer_init :atr1, :atr2
end

atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
# can not use reader
# p atr_sample1.atr1 # => atr1
# p atr_sample1.atr2 # => atr2
atr_sample1.instance_variable_get "@atr1" # => atr1
atr_sample1.instance_variable_get "@atr2" # => atr2

atr_sample2 = AccessorSample.new do |a|
  a.atr1 = 'atr1'
  a.atr2 = 'atr2'
end

# can not use reader
# p atr_sample2.atr1 # => atr1
# p atr_sample2.atr2 # => atr2
atr_sample2.instance_variable_get "@atr1" # => atr1
atr_sample2.instance_variable_get "@atr2" # => atr2

same mean code is

class AccessorSample
  attr_writer :atr1, :atr2

  def initialize(values = nil, &block)
    return yield self if block
    @atr1 = values[:atr1]
    @atr2 = values[:atr2]
  end
end

atr_sample1 = AccessorSample.new :atr1 => 'atr1', :atr2 => 'atr2'
# can not use reader
# p atr_sample1.atr1 # => atr1
# p atr_sample1.atr2 # => atr2
atr_sample1.instance_variable_get "@atr1" # => atr1
atr_sample1.instance_variable_get "@atr2" # => atr2

atr_sample2 = AccessorSample.new do |a|
  a.atr1 = 'atr1'
  a.atr2 = 'atr2'
end
# can not use reader
# p atr_sample2.atr1 # => atr1
# p atr_sample2.atr2 # => atr2
atr_sample2.instance_variable_get "@atr1" # => atr1
atr_sample2.instance_variable_get "@atr2" # => atr2

back to list

EndERB.apply

for single template script using END and DATA

sample case

require "end_erb"

def hoge
  hash = {
    hoge: '@hoge@',
    hige: '@hige@',
  }
  EndERB.apply(hash)
end

puts hoge

output

hoge=@hoge@
hige=@hige@

back to list

File.insert_bom

  • this method's main purpose is 'UTF-8 Excel CSV File'.

output bommed text from input.csv to output.csv

require 'tbpgr_utils'

File.insert_bom("input.csv", "output.csv") # => output bommed text to output.csv

output bommed text from input.csv to input.csv

require 'tbpgr_utils'

File.insert_bom("input.csv") # => output bommed text to output.csv

back to list

Ghostable

  • include Ghostable
  • create ghost method by using Ghostable::ghost_method
  • ghost_method first_args = method_name_pattern
  • ghost_method second_args = method_base_name Symbol(using in Ghostable internal logic)
  • ghost_method third = block. this block is main logic. block can use args[method_name, *args, &block]

sample ghost method define module.

require 'ghostable'
module Checkable
  include Ghostable
  ghost_method /check_range_.*\?$/, :check_range do |method_name, *args, &block|
    method_name.to_s =~ /(check_range_)(\d+)(_to_)(\d*)/
    from = $2.to_i
    to = $4.to_i
    value = args.first
    (from..to).include? value
  end

  ghost_method /^contain_.*\?$/, :check_contain do |method_name, *args, &block|
    method_name.to_s =~ /^(contain_)(.*)(\?)/
    word = $2
    value = args.first
    value.include? word
  end
end
  • use ghost method

sample ghost method use class

class SampleChecker
  include Checkable
end

sample = SampleChecker.new
sample.check_range_3_to_5?(4) # => return true
sample.check_range_3_to_5?(6) # => return false
sample.check_range_3_to_6?(6) # => return true

sample.contain_hoge? "test_hoge_test" # => return true
sample.contain_hoge? "test_hige_test" # => return false
sample.contain_hige? "test_hige_test" # => return true

back to list

Kernel#capture_stdout

capture STDOUT to String. This method can use in STDOUT contents test.

require 'test_toolbox'

result = capture_stdout {puts "test"} # => "test"

# no stdout case. return empty.
result = capture_stdout {sleep 0.1} # => ""(empty)

back to list

Kernel#dp_line

debug print line for print-debugging.

require 'test_toolbox'

# default usage
dp_line __LINE__
# output is following. yy = line no.
# => --------------------|filename=|line=yy|--------------------\n

# output with filename
dp_line __LINE__, filename: __FILE__
# output is following. xx=filenamem, yy = line no.
# => --------------------|filename=xx|line=yy|--------------------\n

# output with specific line charactor.
dp_line __LINE__, filename: __FILE__, char: '@'
# output is following. xx=filenamem, yy = line no.
# => @@@@@@@@@@@@@@@@@@@@|filename=xx|line=yy$|@@@@@@@@@@@@@@@@@@@@\n

back to list

Kernel#bulk_define_methods

Define methods to classes. Methods have simple return value.

require 'tbpgr_utils'
bulk_define_methods [NilClass, FalseClass], :blank?, true
bulk_define_methods [TrueClass, Numeric], "blank?", false

puts nil.blank?   # => true
puts false.blank? # => true
puts true.blank?  # => false
puts 1.blank?     # => false

bulk_define_methods [NilClass, FalseClass], [:blank?, :present?], [true, false]
bulk_define_methods [TrueClass, Numeric], [:blank?, :present?], [false, true]

puts nil.blank?     # => true
puts nil.present?   # => false
puts false.blank?   # => true
puts false.present? # => false
puts true.blank?    # => false
puts true.present?  # => true
puts 1.blank?       # => false
puts 1.present?     # => true

if you don't use bulk_define_methods, followinng code is same mean.

class NilClass
 def blank?
   true
 end

 def present?
   false
 end
end

class FalseClass
 def blank?
   true
 end

 def present?
   false
 end
end

back to list

Kernel#aa_ancestors

Ascii Airt Ancestors

class BaseHogeForAncestors;end
class HogeForAncestors < BaseHogeForAncestors;end

puts HogeForAncestors.aa_ancestors

result is ...

----------------------
|     BasicObject    |
----------------------
          |
----------------------
|       Kernel       |
----------------------
          |
----------------------
|       Object       |
----------------------
          |
----------------------
|BaseHogeForAncestors|
----------------------
          |
----------------------
|  HogeForAncestors  |
----------------------

back to list

Kernel#print_eval

This method for sample code. for manual, for blog-entry's-snippet ...etc.

print_eval 8/4, binding  # => 8/4 # => 2

message = 'msg'
print_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"

output

8/4 # => 2"hoge-#{message}" # => "hoge-msg"

back to list

Kernel#puts_eval

This method for sample code. for manual, for blog-entry's-snippet ...etc.

puts_eval 8/4, binding

message = 'msg'
puts_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"

output

8/4 # => 2
"hoge-#{message}" # => "hoge-msg"

back to list

Kernel#bulk_puts_eval

multi line version of puts_eval.

message = "msg"
bulk_puts_eval binding, "\"hoge-hige1\" + \"add\" + message\n\"hoge-hige2\" + \"add\" + message\n"

output

"hoge-hige1" + "add" + message # => "hoge-hige1addmsg"
"hoge-hige2" + "add" + message # => "hoge-hige2addmsg"

back to list

EvalHelper Object

enable to use EvalHelper in Object

require 'eval_helper_object'
require_code("hoge") # => 'require "hoge"'

back to list

EvalHelper#each_do_code

require 'eval_helper'
class EvalHelperEacjBraceTest
  include EvalHelper

  def hoge(hash)
    each_do_code(hash[:target], hash[:proc])
  end
end

hash = {
  target: '[:a, :b]',
  proc: "puts \"\#{v}1\"\nputs \"\#{v}2\"\n",
}
EvalHelperEacjBraceTest.new.hoge(hash) # => return "[:a, :b].each do |v|\n  puts \"\#{v}1\"\n  puts \"\#{v}2\"\nend"

back to list

EvalHelper#each_brace_code

require 'eval_helper'
class EvalHelperEacjBraceTest
  include EvalHelper

  def hoge(hash)
    each_brace_code(hash[:target], hash[:proc])
  end
end

hash = {
  target: '[:a, :b]',
  proc: 'puts v',
}
EvalHelperEacjBraceTest.new.hoge(hash) # => return '[:a, :b].each { |v|puts v }'

back to list

EvalHelper#each_with_index_brace_code

require 'eval_helper'
class EvalHelperEachWithIndexBraceTest
  include EvalHelper

  def hoge(hash)
    each_with_index_brace_code(hash[:target], hash[:proc])
  end
end

hash = {
  target: '[:a, :b]',
  proc: 'puts "#{i}:#{v}"',
}
EvalHelperEachWithIndexBraceTest.new.hoge(hash) # => return '[:a, :b].each { |v, i|puts "#{i}:#{v}" }'

back to list

EvalHelper#if_code

if case

require 'eval_helper'
class EvalHelperTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = if_code(hash[:if_cond], hash[:if_proc], hash[:else_proc])
    instance_eval code
  end
end

hash = {
  input: "test",
  if_cond: "msg == 'test'",
  if_proc: "true",
  else_proc: "false",
}
EvalHelperTest.new.hoge(hash) # => return true

else case

require 'eval_helper'
class EvalHelperTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = if_code(hash[:if_cond], hash[:if_proc], hash[:else_proc])
    instance_eval code
  end
end

hash = {
  input: "not_test",
  if_cond: "msg == 'test'",
  if_proc: "true",
  else_proc: "false",
}
EvalHelperTest.new.hoge(hash) # => return false

back to list

EvalHelper#if_code_after

if case

require 'eval_helper'

class EvalHelperTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = if_code_after(hash[:if_cond], hash[:if_proc])
    ret = 'dafault'
    instance_eval code
    ret
  end
end

hash = {
  input: "test",
  if_cond: "msg == 'test'",
  if_proc: "ret = 'true'",
}
EvalHelperTest.new.hoge(hash) # => return 'true'

else case

require 'eval_helper'

class EvalHelperTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = if_code_after(hash[:if_cond], hash[:if_proc])
    ret = 'ret = "true"'
    instance_eval code
    ret
  end
end

hash = {
  input: "not_test",
  if_cond: "msg == 'test'",
  if_proc: "ret = 'true'",
}
EvalHelperTest.new.hoge(hash) # => return 'default'

back to list

EvalHelper#require_code

single require case

require 'eval_helper'
class EvalHelperRequireTest
  include EvalHelper

  def hoge(*args)
    require_code(args)
  end
end

args = 'tbpgr_utils'
EvalHelperRequireTest.new.hoge(args) # => return "require 'tbpgr_utils'\n"

muiti require case

require 'eval_helper'
class EvalHelperRequireTest
  include EvalHelper

  def hoge(*args)
    require_code(args)
  end
end

args =  ['tbpgr_utils', 'eval_helper']
EvalHelperRequireTest.new.hoge(args) # => return "require 'tbpgr_utils'\nrequire 'eval_helper'\n"

back to list

EvalHelper#require_relative_code

single require_relative case

require 'eval_helper'
class EvalHelperRequireRelativeTest
  include EvalHelper

  def hoge(*args)
    require_relative_code(args)
  end
end

args = 'tbpgr_utils'
EvalHelperRequireRelativeTest.new.hoge(args) # => return "require_relative 'tbpgr_utils'\n"

muiti require_relative case

require 'eval_helper'
class EvalHelperRequireRelativeTest
  include EvalHelper

  def hoge(*args)
    require_relative_code(args)
  end
end

args =  ['tbpgr_utils', 'eval_helper']
EvalHelperRequireRelativeTest.new.hoge(args) # => return "require_relative 'tbpgr_utils'\nrequire_relative 'eval_helper'\n"

back to list

EvalHelper#set_variable_code

set string variable case

require 'eval_helper'
class EvalHelperSetVariableTest
  include EvalHelper

  def hoge(name, value)
    set_variable_code(name, value)
  end
end

hash = {
  name: 'hoge',
  value: '"hoge"',
}
EvalHelperSetVariableTest.new.hoge(hash[:name], hash[:value])

return

hoge = "hoge"

set numeric variable case

require 'eval_helper'
class EvalHelperSetVariableTest
  include EvalHelper

  def hoge(name, value)
    set_variable_code(name, value)
  end
end

hash = {
  name: 'hoge_num',
  value: '1',
}
EvalHelperSetVariableTest.new.hoge(hash[:name], hash[:value])

return

hoge_num = 1

back to list

EvalHelper#set_variables_code

require 'eval_helper'
class EvalHelperSetVariablesTest
  include EvalHelper

  def hoge(variables)
    set_variables_code(variables)
  end
end

variables = [
  {
    name: 'name1',
    value: '"value1"',
  },
  {
    name: 'name2',
    value: '"value2"',
  },
]
EvalHelperSetVariablesTest.new.hoge(variables)

back to list

EvalHelper#times_code

single_line_proc case

require 'eval_helper'

class EvalHelperTimesTest
  include EvalHelper

  def hoge(number, proc)
    times_code(number, proc)
  end
end

hash = {
  number: 2,
  proc: 'puts "#{i}times"',
}
EvalHelperTimesTest.new.hoge(hash[:number], hash[:proc])

return

2.times { |i| puts "#{i}times" }

multi_line_proc case

require 'eval_helper'

class EvalHelperTimesTest
  include EvalHelper

  def hoge(number, proc)
    times_code(number, proc)
  end
end

hash = {
  number: 3,
  proc: 'puts "#{i}times"\nputs "#{i*2}times"',
}
EvalHelperTimesTest.new.hoge(hash[:number], hash[:proc])

return

3.times do |i|
  puts "#{i}times"
  puts "#{i*2}times"
end

back to list

EvalHelper#ternary_operator

true case

require 'eval_helper'

class EvalHelperTernaryTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = \
      if hash[:ret]
        ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
      else
        ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
      end
    instance_eval code
  end
end

hash = {
  input: "test",
  cond: "msg == 'test'",
  true_case: "true",
  false_case: "false",
  ret: "ret",
}
EvalHelperTernaryTest.new.hoge(hash) # => return 'true'

false case

require 'eval_helper'

class EvalHelperTernaryTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = \
      if hash[:ret]
        ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
      else
        ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
      end
    instance_eval code
  end
end

hash = {
  input: "not_test",
  cond: "msg == 'test'",
  true_case: "true",
  false_case: "false",
  ret: "ret",
}
EvalHelperTernaryTest.new.hoge(hash) # => return 'false'

back to list

EvalHelper#unless_code

unless case

require 'eval_helper'
class EvalHelperTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = unless_code(hash[:unless_cond], hash[:unless_proc], hash[:else_proc])
    instance_eval code
  end
end

hash = {
  input: "not_test",
  unless_cond: "msg == 'test'",
  unless_proc: "true",
  else_proc: "false",
}
EvalHelperTest.new.hoge(hash) # => return true

else case

require 'eval_helper'
class EvalHelperTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = unless_code(hash[:unless_cond], hash[:unless_proc], hash[:else_proc])
    instance_eval code
  end
end

hash = {
  input: "test",
  unless_cond: "msg == 'test'",
  unless_proc: "true",
  else_proc: "false",
}
EvalHelperTest.new.hoge(hash) # => return false

back to list

EvalHelper#unless_code_after

unless case

require 'eval_helper'

class EvalHelperTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = unless_code_after(hash[:unless_cond], hash[:unless_proc])
    ret = 'dafault'
    instance_eval code
    ret
  end
end

hash = {
  input: "not_test",
  unless_cond: "msg == 'test'",
  unless_proc: "ret = 'true'",
}
EvalHelperTest.new.hoge(hash) # => return 'true'

else case

require 'eval_helper'

class EvalHelperTest
  include EvalHelper

  def hoge(hash)
    msg = hash[:input]
    code = unless_code_after(hash[:unless_cond], hash[:unless_proc])
    ret = 'ret = "true"'
    instance_eval code
    ret
  end
end

hash = {
  input: "test",
  unless_cond: "msg == 'test'",
  unless_proc: "ret = 'true'",
}
EvalHelperTest.new.hoge(hash) # => return 'default'

back to list

MetasyntacticVariable

  • META variable
MetasyntacticVariable::META_VARIABLES  # => [:foo, :bar, :baz, :qux, :quux, :corge, :grault, :garply, :waldo, :fred, :plugh, :xyzzy, :thud]
MetasyntacticVariable.meta_variables  # => [:foo, :bar, :baz, :qux, :quux, :corge, :grault, :garply, :waldo, :fred, :plugh, :xyzzy, :thud]
  • META variable for classes
MetasyntacticVariable::META_CLASSES  # => [:foo, :bar, :baz, :qux, :quux, :corge, :grault, :garply, :waldo, :fred, :plugh, :xyzzy, :thud]
MetasyntacticVariable.meta_classes  # => [:foo, :bar, :baz, :qux, :quux, :corge, :grault, :garply, :waldo, :fred, :plugh, :xyzzy, :thud]

back to list

Module.alias_methods

create alias methods.

require "tbpgr_utils"

class Hoge
  def hoge
    "hoge"
  end

  alias_methods [:hige, :hege, :huge], :hoge
end

Hoge.new.hoge # => "hoge"
Hoge.new.hige # => "hoge"
Hoge.new.hege # => "hoge"
Hoge.new.huge # => "hoge"

same code is...

class Hoge
  def hoge
    "hoge"
  end

  alias_method :hige, :hoge
  alias_method :hege, :hoge
  alias_method :huge, :hoge
end

back to list

Object#any_of?

require 'tbpgr_utils'

p 'hoge'.any_of? 'hoge', 'hige'    # =>true
p 'hoge'.any_of?(*%w{hoge hige})    # =>true
p 'hige'.any_of? 'hoge', 'hige'    # =>true
p 'hege'.any_of? 'hoge', 'hige'    # =>false
p 1.any_of? 1, 2, 3                # =>true
p 4.any_of? 1, 2, 3                # =>false

back to list

Object#boolean?

require 'tbpgr_utils'

p true.boolean?    # =>true
p false.boolean?   # =>true
p nil.boolean?     # =>false
p "".boolean?      # =>false
p "true".boolean?  # =>false

back to list

Object#guard

guard return case

def hoge(msg)
  guard(msg) {return "guard"}
  "not guard"
end

hoge true # => "guard"
hoge false # => "not guard"

guard fail case

def hoge(msg)
  guard(msg) {fail ArgumentError, 'error!!'}
  "not guard"
end

hoge true # => raise ArgumentError. message = error!!
hoge false # => "not guard"

back to list

Object#unless_guard

unless_guard return case

def hoge(msg)
  unless_guard(msg) {return "unless_guard"}
  "not unless_guard"
end

hoge false # => "unless_guard"
hoge true # => "not unless_guard"

unless_guard fail case

def hoge(msg)
  unless_guard(msg) {fail ArgumentError, 'error!!'}
  "not unless_guard"
end

hoge false # => raise ArgumentError. message = error!!
hoge true # => "not unless_guard"

back to list

Object#my_methods

require 'tbpgr_utils'

class Hoge
  def hgoe
  end

  protected
  def hige
  end

  private
  def hege
  end
end

p Hoge.new.my_methods # =>[:hoge, :hige, :hege]

back to list

Object#to_bool

require 'tbpgr_utils'

p true.to_bool # => true
p false.to_bool # => false
p 'true'.to_bool # => true
p 'false'.to_bool # => true
p nil.to_bool # => false
p 0.to_bool # => true

back to list

String#comma_to_a

space commma case

require 'tbpgr_utils'
'1, 5, 9'.comma_to_a # => ["1", "5", "9"]

commma case

require 'tbpgr_utils'
'1,5,9'.comma_to_a # => ["1", "5", "9"]

back to list

String#hyphen_to_a

number case

require 'tbpgr_utils'
'1-5'.hyphen_to_a # => [1, 2, 3, 4, 5]

alphabet case

require 'tbpgr_utils'
'"a"-"e"'.hyphen_to_a # => ['a', 'b', 'c', 'd', 'e']

back to list

String#justify_table

require 'tbpgr_utils'

str ="|* hogehogehoge|* hege|* hige|\n|test|tester|testest|\n|test|tester|aaaaaaaaaaaaaaaaaaaaaaatestest|\n"

puts str.justify_table

output

|* hogehogehoge|* hage|* hige                        |
|test          |tester|testest                       |
|test          |tester|aaaaaaaaaaaaaaaaaaaaaaatestest|

back to list

String#say

default case

'hoge'.say # => 'hoge'

quote case

'hoge'.say(:quote) # => 'hoge'

dquote case

'hoge'.say(:dquote) # => "hoge"

bracket case

'hoge'.say(:bracket) # => [hoge]

hyphen case

'hoge'.say(:hyphen) # => -hoge-

back to list

String#stripe

default case

require 'tbpgr_utils'

'hoge'.stripe # => HoGe

lower_cap case

require 'tbpgr_utils'

'hoge'.stripe :lower_cap # => hOgE

empty case

require 'tbpgr_utils'

''.stripe # => ''

nil case

require 'tbpgr_utils'

hoge = nil
hoge.stripe # => nil

back to list

String#surround

single line, no option case

require 'tbpgr_utils'
'hoge'.surround

result

------
|hoge|
------

multi line, no option case

require 'tbpgr_utils'
"hoge\na".surround

result

------
|hoge|
|a   |
------

single line, both option case

require 'tbpgr_utils'
'hoge'.surround top_bottom: '=', side: '!'

result

======
!hoge!
======

back to list

String#to_hatena_heading

> case

require 'tbpgr_utils'
'hoge>hige'.to_hatena_heading # => '*hoge\n**hige'

+ case

require 'tbpgr_utils'

'hoge+hige'.to_hatena_heading # => '*hoge\n*hige'

^ case

require 'tbpgr_utils'
'hoge>hige^hege'.to_hatena_heading # => '*hoge\n**hige\n*hege'

back to list

String#to_markdown_heading

> case

require 'tbpgr_utils'
'hoge>hige'.to_markdown_heading # => '# hoge\n## hige'

+ case

require 'tbpgr_utils'

'hoge+hige'.to_markdown_heading # => '# hoge\n# hige'

^ case

require 'tbpgr_utils'
'hoge>hige^hege'.to_markdown_heading # => '# hoge\n## hige\n# hege'

back to list

String#to_space2_heading

> case

require 'tbpgr_utils'
'hoge>hige'.to_space2_heading # => 'hoge\n  hige'

+ case

require 'tbpgr_utils'

'hoge+hige'.to_space2_heading # => 'hoge\nhige'

^ case

require 'tbpgr_utils'
'hoge>hige^hege'.to_space2_heading # => 'hoge\n  hige\nhege'

back to list

String#to_space4_heading

> case

require 'tbpgr_utils'
'hoge>hige'.to_space4_heading # => 'hoge\n    hige'

+ case

require 'tbpgr_utils'
'hoge+hige'.to_space4_heading # => 'hoge\nhige'

^ case

require 'tbpgr_utils'
'hoge>hige^hege'.to_space4_heading # => 'hoge\n    hige\nhege'

back to list

String#to_tab_heading

> case

require 'tbpgr_utils'
'hoge>hige'.to_tab_heading # => 'hoge\n\thige'

+ case

require 'tbpgr_utils'
'hoge+hige'.to_tab_heading # => 'hoge\nhige'

^ case

require 'tbpgr_utils'
'hoge>hige^hege'.to_tab_heading # => 'hoge\n\thige\nhege'

Templatable

  • include Templatable
  • set template by here-document
  • in template, parameter must name 'placeholders[:xxxxx]'. xxxxx is your favorite name.
  • when create instance, you must set materials to create template. after, you can get this value from @materials.
  • you must create manufactured_xxx methods. xxx is each-placeholder name.
  • you can get result by 'result' method.
require 'templatable'

class TemplateUser
  include Templatable
  template "line1:<%=placeholders[:hoge]%>\nline2:<%=placeholders[:hige]%>\n  EOS\n\n  def manufactured_hoge\n    \"hoge-\#{@materials}\"\n  end\n\n  def manufactured_hige\n    \"hige-\#{@materials}\"\n  end\nend\n\np TemplateUser.new('sample').result\n"

output

line1:hoge-sample
line2:hige-sample

back to list

TemplateMethodable

sample usage

require "template_methodable"
# sample BaseClass
class BaseDeveloper
  include TemplateMethodable
  must_impl :easy_coding, :difficult_coding, :normal_coding
  module DIFFICILTY
    EASY = 1
    NORMAL = 2
    DIFFICILT = 3
  end
  def coding(difficulty)
    ret = []
    ret << "start coding"
    case difficulty
    when DIFFICILTY::EASY
      ret << easy_coding
    when DIFFICILTY::NORMAL
      ret << normal_coding
    when DIFFICILTY::DIFFICILT
      ret << difficult_coding
    else
      fail 'error'
    end
    ret << "finish coding"
    ret.join("\n")
  end
end

# sample valid Concrete Class.
class StarDeveloper < BaseDeveloper
  def easy_coding
    "complete 1 minutes"
  end
  def normal_coding
    "complete 10 minutes"
  end
  def difficult_coding
    "complete 59 minutes"
  end
end

# sample invalid Concrete Class. if call NormalDeveloper#difficult_coding, it raises NotImplementedError.
class NormalDeveloper < BaseDeveloper
  def easy_coding
    "complete 10 minutes"
  end
  def normal_coding
    "complete 100 minutes"
  end
end

Relation

if you are Sublime Text2 user, you can use snippet for TbpgrUtils.

https://github.com/tbpgr/tbpgr_utils_snippets

History

  • version 0.0.65 : add EvalHelper#each_with_index_brace_code
  • version 0.0.64 : add EvalHelper#each_do_code
  • version 0.0.63 : add EvalHelper#each_brace_code, String#hyphen_to_a, String#commma_to_a
  • version 0.0.62 : add EvalHelper#set_variables_code
  • version 0.0.61 : add EvalHelper#set_variable_code
  • version 0.0.60 : add EvalHelper#times_code
  • version 0.0.59 : add EvalHelper Object
  • version 0.0.58 : add EvalHelper#require_relative_code
  • version 0.0.57 : add EvalHelper#require_code
  • version 0.0.56 : add EvalHelper#toternary_operator
  • version 0.0.55 : add EvalHelper#unless_code_after
  • version 0.0.54 : add EvalHelper#unless_code
  • version 0.0.53 : add EvalHelper#if_code_after
  • version 0.0.52 : add EvalHelper#if_code
  • version 0.0.51 : add String#to_hatena_heading
  • version 0.0.50 : add String#to_markdown_heading
  • version 0.0.49 : add String#to_tab_heading
  • version 0.0.48 : add String#to_space4_heading
  • version 0.0.47 : add String#to_space2_heading
  • version 0.0.46 : add String#stripe
  • version 0.0.45 : add String#say
  • version 0.0.44 : add EndERB.apply
  • version 0.0.43 : add Array#together_slice(alias tslice).
  • version 0.0.42 : add MetasyntacticVariable
  • version 0.0.41 : add Object#guard, unless_guard
  • version 0.0.40 : add Kernel#aa_ancestors.
  • version 0.0.39 : add String#surround.
  • version 0.0.38 : add Array#together_shuffle(alias tshuffle).
  • version 0.0.37 : add Array#together_sample(alias tsample).
  • version 0.0.36 : add Array#together_reverse,Array#together_reverse!(alias treverse, alias treverse!).
  • version 0.0.35 : add Array#together_pop(alias tpop).
  • version 0.0.34 : add Array#together_last(alias tlast).
  • version 0.0.33 : add Array#together_shift(alias tshift).
  • version 0.0.32 : add Array#together_insert(alias tinsert).
  • version 0.0.31 : add Array#together_index(alias tindex).
  • version 0.0.30 : add Array#together_include?(alias tinclude?).
  • version 0.0.29 : add Array#together_first(alias tfirst).
  • version 0.0.28 : add Array#together_fill(alias tfill). add File.insert_bom.
  • version 0.0.27 : add Array#together_empty?(alias tempty?)
  • version 0.0.26 : add Array#together_delete_if(alias tdelete_if)
  • version 0.0.25 : add Array#together_delete_at(alias tdelete_at)
  • version 0.0.24 : add Array#together_delete(alias tdelete)
  • version 0.0.23 : add Array#together_map!(aliases => [tmap!, together_collect!, tcollect!])
  • version 0.0.22 : add Array#together_compact. together_compact has alias :tcompact. Array#together_compact!. together_compact! has alias :tcompact!.
  • version 0.0.21 : add Array#together_clear. together_clear has alias :tclear
  • version 0.0.20 : add Array#together_at. together_at has alias :tat
  • version 0.0.19 : add AttributesHashable module.
  • version 0.0.18 : add Array#together_concat. together_concat has alias tconcat
  • version 0.0.17 : add Array#together_reduce(or treduce, together_inject, tinject)
  • version 0.0.16 : add Array#together_select(or tselect, together_find_all, tfindall)
  • version 0.0.15 : add Module.alias_methods
  • version 0.0.14 : add Array#together_map(aliases => [tmap, together_collect, tcollect])
  • version 0.0.13 : add Array#together_with_index, Kernel#bulk_puts_eval
  • version 0.0.12 : AttributesInitializable::ClassMethods.attr_reader_init,attr_writer_init
  • version 0.0.11 : add Object#to_bool.
  • version 0.0.10 : add TemplateMethodable module.
  • version 0.0.9 : add TestToolbox module. add Kernel#capture_stdout, Kernel#dp_line
  • version 0.0.8 : add Kernel#bulk_define_methods
  • version 0.0.7 : add Kernel#print_eval, Kernel#puts_eval
  • version 0.0.6 : add Ghostable
  • version 0.0.5 : add Templatable
  • version 0.0.4 : AttributesInitializable::ClassMethods.attr_accessor_init
  • version 0.0.3 : add Object#any_of?
  • version 0.0.2 : loop all arrays by block.
  • version 0.0.1 : first release.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request