Your SlideShare is downloading. ×
How To Use Kagemusha
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

How To Use Kagemusha

1,717
views

Published on

ruby library kagemusha

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
1,717
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. How to Use “Kagemushaquot; 2007-09-29 Tatsuhiro Ujihisa at Ruby Kansai Workshop#19 1
  • 2. ✦ Tatsuhiro Ujihisa ✦ ruby, haskell, lisp, javascript ✦ Fuzzy Rough Sets Theory ✦ using ruby and haskell ✦ writer of Kagemusha README 2
  • 3. sorry for my not-good-English. 3
  • 4. Kagemusha 4
  • 5. What ✦ “Kagemusha is a library of helper functions for testing Ruby scripts.quot; -- Kagemusha README 5
  • 6. When ✦ making mock-class for tests ✦ modifying base-class with scoped 6
  • 7. Who ✦ Yuya Kato ✦ My teacher of Ruby 7
  • 8. Are you a beginner or an advanced rubyist? 8
  • 9. Advanced rubyists: ✦ Let's read source codes of Kagemusha, without listening this presentation. ✦ Enjoy Hacking! 9
  • 10. Beginners: ✦ Here we go. 10
  • 11. class Fixnum def to_s quot;ujihisaquot; end end puts 23412 #=> ujihisa Let's try on IRB 11
  • 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. 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. 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. Remember these: ๏ requires ๏ Kagemusha.new ๏ Kagemusha#def, defs ๏ Kagemusha#swap That's All. 15
  • 16. Demonstrate ✦ Kernel#rand, Time.now, etc. ✦ on Mac OSX, zsh, screen, vim, ruby-1.8, Kagemusha-0.0.3 16
  • 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. ✦ You can change/add any methods in the block. ✦ You can write simple, safety and clean code. 18
  • 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. 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. Exercise String#to_i returns 0 Fixnum#+ means minus Hash#map returns not array but hash 21
  • 22. That's all. Thank you! Kagemusha 22