How To Use Kagemusha

2,031 views

Published on

ruby library kagemusha

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,031
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
32
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

How To Use Kagemusha

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

×