Ruby on Windows (RubyConf.tw 2011)

30
RUBY ON WINDOWS Up and Runing 1 Saturday, August 27, 11

description

Small intro about using Ruby on Windows.

Transcript of Ruby on Windows (RubyConf.tw 2011)

Page 1: Ruby on Windows (RubyConf.tw 2011)

RUBY ON WINDOWSUp and Runing

1Saturday, August 27, 11

Page 2: Ruby on Windows (RubyConf.tw 2011)

WHO AM I?

2Saturday, August 27, 11

Page 3: Ruby on Windows (RubyConf.tw 2011)

About me

張銘軒 (Mason Chang)

Was a hardware engineer

Love software and programming

Start using Ruby since late 2009

Twitter: @changmason (I don’t tweet though)

Email: [email protected]

3Saturday, August 27, 11

Page 4: Ruby on Windows (RubyConf.tw 2011)

WHY (ON WINDOWS)?

4Saturday, August 27, 11

Page 5: Ruby on Windows (RubyConf.tw 2011)

You are more familiar with Windows environments.

Picture courtesy of yum9me (CC) BY-NC-ND 2.0 http://www.flickr.com/photos/yum9me/532905569

5Saturday, August 27, 11

Page 6: Ruby on Windows (RubyConf.tw 2011)

You don’t want to fight against existing tools and workflows inside your company.

Picture courtesy of erix (CC) BY 2.0http://www.flickr.com/photos/erix/77966968/

6Saturday, August 27, 11

Page 7: Ruby on Windows (RubyConf.tw 2011)

Broader user base, your target users are on Windows.

Picture courtesy of opacity (CC) BY-NC-ND 2.0http://www.flickr.com/photos/opacity/4483057728/

7Saturday, August 27, 11

Page 8: Ruby on Windows (RubyConf.tw 2011)

WHAT RUBY?... BECAUSE

8Saturday, August 27, 11

Page 9: Ruby on Windows (RubyConf.tw 2011)

Many Rubies (which on Win?)

◎ => Yes. ✕ => No. ▵ => Maybe. (planned to support Windows in the near future)

MRI JRuby IronRuby Rubinius MacRuby MagLev

◎ ◎ ◎ ▵ ✕ ✕

9Saturday, August 27, 11

Page 10: Ruby on Windows (RubyConf.tw 2011)

BUT, Windows users don’t like to compile code!!!

Picture courtesy of TahoeSunsets (CC) BY-NC-SA 2.0http://www.flickr.com/photos/raparker/236912028

10Saturday, August 27, 11

Page 11: Ruby on Windows (RubyConf.tw 2011)

Ruby InstallerCompiled executables under MinGW32 dev environment

Both newest Ruby 1.8.7p352 and 1.9.2p290 are available

11Saturday, August 27, 11

Page 12: Ruby on Windows (RubyConf.tw 2011)

JRuby InstallerRequire JRE (or JDK if you want to compile .rb into .class)

Ruby 1.8 compatible and Ruby 1.9 supported

12Saturday, August 27, 11

Page 13: Ruby on Windows (RubyConf.tw 2011)

Rails InstallerAll in one package, including MRI Ruby, Rails, Git and DevKit

13Saturday, August 27, 11

Page 14: Ruby on Windows (RubyConf.tw 2011)

rvm => No, pik => Yes

Very similar to rvm, it helps you to install, manage, and switch among all available Rubies on Windows.

There are two ways to install pik:

1. If you already have a Ruby installed: gem install pik pik_install c:\bin2.otherwise, use pik installer: https://github.com/vertiginous/pik/downloads

14Saturday, August 27, 11

Page 15: Ruby on Windows (RubyConf.tw 2011)

pik help commands

15Saturday, August 27, 11

Page 16: Ruby on Windows (RubyConf.tw 2011)

Other Dev Tools

Version control: msysgit: http://code.google.com/p/msysgit/

git-bash: Provide a *nix-like CLI with colors on Windows.

Text editors: Notepad++: http://notepad-plus-plus.org/ (Freeware)E-texteditor: http://www.e-texteditor.com/ (Textmate clone)

16Saturday, August 27, 11

Page 17: Ruby on Windows (RubyConf.tw 2011)

HOW TO ?

17Saturday, August 27, 11

Page 18: Ruby on Windows (RubyConf.tw 2011)

How to code?

EOL(end of line):To share your code with the community, please convert EOL from Windows format(CRLF) to UNIX format(LF) in your source files. (You text editors can do this for you, check it!!)

Pathname:Although Ruby, on Windows, can automatically translate in between “/” and “\” of a file path, it is recommended that you use “pathname” to handle a complex file path which is got from the user. (There may be spaces and Chinese characters.)

18Saturday, August 27, 11

Page 19: Ruby on Windows (RubyConf.tw 2011)

How to install gems?“gem install xxx” will automatically detect your platform and install the right gem for you.

For JRuby, please use “jruby -S gem install xxx” instead.

You can also manually download and install the gems that correspond toyour platform if “gem install xxx” failto detect the right gems for you.

19Saturday, August 27, 11

Page 20: Ruby on Windows (RubyConf.tw 2011)

How about gem compatibility?

Most gems written in pure Ruby or shipped with precompiledC-extension work out of the box on Windows: sinatra, nokogiri

Some gems with native C-extension may need a few tweaks and compilation(DevKit is your friend): mysql2

Still there are very few gems that will hardly work on Windows because they are designed for POSIX OS: unicorn

20Saturday, August 27, 11

Page 21: Ruby on Windows (RubyConf.tw 2011)

How to do Windows automation(use WIN32OLE)?

Operate Windows applications just like operate Ruby objects, (no need to write any VB script):InternetExplorer, Excel, Word, PowerPoint...

Reflection support through methods with prefix ole_*::ole_methods, :ole_type, :ole_method_help

OLE object’s OLE methods are case insensitive:obj.visible is the same as object.ViSiBlE

Please refer to MSDN for API: http://msdn.microsoft.com/

21Saturday, August 27, 11

Page 22: Ruby on Windows (RubyConf.tw 2011)

WIN32OLE - examplehttps://gist.github.com/1173715

22Saturday, August 27, 11

Page 23: Ruby on Windows (RubyConf.tw 2011)

How to program GUI?

Several libraries are available:FoxRuby, WxRuby, Shoes, Bowline

However, these gems are not under regular maintenance

GUI programming with Swing in JRuby is a good solution

23Saturday, August 27, 11

Page 24: Ruby on Windows (RubyConf.tw 2011)

Swing GUI - example(p1)https://github.com/changmason/id_maker

24Saturday, August 27, 11

Page 25: Ruby on Windows (RubyConf.tw 2011)

Swing GUI - example(p2)import Java classes to current Ruby namespace

Ruby class can inherit Java class

setter in Java style

setter in Ruby style

event handling by a Ruby block

tell the swing worker to run in background

call Java’s constructor

25Saturday, August 27, 11

Page 26: Ruby on Windows (RubyConf.tw 2011)

Swing GUI - example(p3)Although JRuby runtime can call Java methods in camelCaseStyle or in snake_case_style, this doInBackground method is invoked by Java in the background thread actually. So we MUST name it in Java way.

26Saturday, August 27, 11

Page 27: Ruby on Windows (RubyConf.tw 2011)

How about packaging your programs?

In Ruby use OCRA (One Click Ruby Application):Package all your source files with the Ruby interpreter and required gems into one .exe file.

In JRuby use rawr or Warbler:Same as above, but they can also help you to compile your .rb files into .class files. The output can be a standalone .jar, .war or .exe file.

27Saturday, August 27, 11

Page 28: Ruby on Windows (RubyConf.tw 2011)

CONCLUSION

28Saturday, August 27, 11

Page 29: Ruby on Windows (RubyConf.tw 2011)

(J)Ruby on Windows is already good enough to learn and play with. We can get more Windows users to join this community.

Ruby's concise syntax and direct access to OLE objectsmake it a powerful productivity tool on Windows.

Stick to JRuby/Java technologies if you really care about cross-platform compatibilities of your programs.

Eventually, you would buy a Mac or setup a Linux machine ifdeveloping (Rails) on Windows really hurts your productivity.

29Saturday, August 27, 11

Page 30: Ruby on Windows (RubyConf.tw 2011)

Q & A

30Saturday, August 27, 11