how to use kagemusha
DESCRIPTION
ruby library kagemushaTRANSCRIPT
How to Use “Kagemusha"
2007-09-29 Tatsuhiro Ujihisaat Ruby Kansai Workshop#19
1
✦ Tatsuhiro Ujihisa✦ ruby, haskell, lisp, javascript✦ Fuzzy Rough Sets Theory
✦ using ruby and haskell ✦ writer of Kagemusha README
2
sorry for my not-good-English.3
Kagemusha
4
What
✦ “Kagemusha is a library of helper functions for testing Ruby scripts."
-- Kagemusha README
5
When
✦ making mock-class for tests✦ modifying base-class with
scoped
6
Who
✦ Yuya Kato✦ My teacher of Ruby
7
Are you a beginner or an advanced rubyist?
8
Advanced rubyists:
✦ Let's read source codes of Kagemusha, without listening this presentation.
✦ Enjoy Hacking!
9
Beginners:
✦ Here we go.
10
class Fixnum def to_s "ujihisa" endend
puts 23412#=> ujihisa
Let's try on IRB11
Open Class
✦ global side-effects "abc" #=> 1:abc #=> nil3 #=> "a"3 + 5 #=> "aa"
etc...It's crazy.
»Open Class with Scope?12
Kagemusha
require 'rubygems'require 'kagemusha'
m = Kagemusha.new Fixnumm.def :to_s do "ujihisa"endm.swap do # write what you want to do puts 1end
13
class Fixnum def to_s "ujihisa" endend
puts 23412#=> ujihisa
require 'rubygems'require 'kagemusha'
m = Kagemusha.new Fixnumm.def :to_s do "ujihisa"endm.swap do # write what you want to do puts 1end
14
Remember these:
๏ requires๏ Kagemusha.new๏ Kagemusha#def, defs๏ Kagemusha#swap
That's All.15
Demonstrate
✦ Kernel#rand, Time.now, etc.✦ on Mac OSX, zsh, screen, vim,
ruby-1.8, Kagemusha-0.0.3
16
Open Class Kagemusha
require 'rubygems'require 'kagemusha'
class Fixnum m = Kagemusha.new Fixnum
def add(i) self + iend
m.def :to_s do |i| self + iend
def self.add(i, j) i + jend
m.defs :add do |i, j| i + jend
17
✦ You can change/add any methods in the block.
✦ You can write simple, safety and clean code.
18
Kagemusha.new(A) do |m| m.def(:f) {|c| puts c } m.swap do a = A.new a.f 'blocked style' endend
Other styles
Kagemusha.new(A). def(:f) {|c| puts c }. swap do a = A.new a.f 'chained style' end
19
Default Mushas
✦ date, datetime, rand, time
require 'rubygems'require 'kagemusha/rand'
Kagemusha::Rand.always(1) do p rand(100) p rand(100)end
You have only to require these.
20
Exercise
String#to_i returns 0
Fixnum#+ means minus
Hash#map returns notarray but hash
21
Kagemusha
That's all. Thank you!
22