reality
Subscribe
tag

Building a iOS Ruby REPL and Opal for iOS

  • Francis Chong

Francis Chong

Jan 12, 2014 • 2 min read
Building a iOS Ruby REPL and Opal for iOS

I'm happy to tell you I just finished the first version of IGJavaScriptConsole, a JavaScript/Ruby REPL for your Objective-C apps!

REPL stands for Read-Eval-Print-Loop. See it in live below:

console

Why?

Since iOS 7 shipped with JavaScriptCore, I always want to run Ruby on it with
Opal. While it is totally possible to write Opal app as is, its getting harder when the Ruby classes getting interact with native classes -- you
must test it on device/simulator but compile-deploy time is so long and so painful.

While there is a way to connect Safari Web Inspector with JSContext, its not possible for Opal: You have to compile it before it can run on JSContext.

Implementation

That make me think why not build a console myself? Thats the beginning of IGJavaScriptConsole.

Its build on top of following:

  • Opal A Ruby to JavaScript compiler. It even come with sprockets extension which lets you bundle the compiled script easily.
  • JavaScriptCoreOpalAdditions A thin layer of Objective-C that load and provides native features to Opal.
  • jqConsole Web based terminal.
  • Ace Web based code editor with syntax highlighting.
  • CocoaHTTPServer HTTP and WebSocket server for realtime communication
    between iOS and desktop.

Opal on iOS, is it practical?

Absolutely.

When programming Opal you first wrap native JavaScript code with Ruby classes.
When doing it in iOS its no different, just remeber you must expose native Objective-C methods to JavaScript with JSExport protocol first, then wrap them with Ruby code.


// expose Objective-C code to JSContext
@protocol ViewControllerExport <JSExport>

-(void) setGreeting:(NSString*)helloText;
-(NSString*) getGreeting;

@end


// Create javascript object from Objective-C
if (!context[@"App"]) {
    context[@"App"] = @{};
}
context[@"App"][@"viewController"] = self;

# wrapping a Objective-C classes
class ViewController
  include Native

  alias_native :greeting, :getGreeting
  alias_native :greeting=, :setGreeting
end
# use the wrapper class
vc = ViewController.new(`App.viewController`)
vc.greeting = "Hi!"

All of those wrapping is boring, but when its done, you can build higher level API or
DSL that dramatically changes how you build apps.

Note you'll not implement the whole app in Ruby with Opal (to do that, use RubyMotion), its best for macro or other dynamic behavior.

If you want to build your own Ruby library for Opal on iOS, check IGHTMLQuery, it wrap everything in Ruby and I think its a pleasure to work with it.

What Next?

  • Implement Ruby standard library that is missing on Opal (due to platform limitation) but critical on native app development.
  • Something like BubbleWrap might also needed to wrap native class to allow more interaction between Ruby and Objective-C.
  • Build great apps and profit!!

Sign up for more like this.

Enter your email
Subscribe
第一次自組水冷

第一次自組水冷

計劃中和做得不錯的部份 1. 我事前畫了大量的設計圖,不同的水管角度也畫了無數次,雖然實際做時也需要隨機應變,但這些設計很有幫助。 2. 因應設計買了足夠的 fittings 和轉角接口,90 度最有用,45度和180度也十分有用。 3. 設計時留了一個排水口,平時隱藏但維護時很有用 計畫外和錯誤的部份 1. 買配件時沒有計算大小,我的機箱是 Fractal Design Define C 本來勉強可以放進 360 + 240 散熱器,但同時配 2 張 GPU 和 D5 水泵就太難搞了。最終我買了大一號的 Define S2。(結果漂亮的 Define C 變成給老婆的新機,這是後話) 2. 買錯了 GPU Block ,看到寫的是 EVGA 1080
Jan 27, 2019 — 3 min read
黑蘋果 iMac Pro

黑蘋果 iMac Pro

上回說到我想自組一台黑蘋果。參考 tonymac 的一篇教學 和相關教程後我終於決定拍板了。現在這台電腦已經成功穩定運行一星期了。這一篇是回顧和記錄,希望其他有需要的朋友會有用。 不過我得強調,除非你像我打算建立一台比 iMac Pro 更靈活更強的電腦,又不怕花一點時間研究,否則去買一台 iMac Pro 相信是個更好的選擇。 效能比較 話說組裝這部電腦是因為在公司用 18 Core iMac Pro 後覺得家中的 5K iMac (2014) 特別慢。一完成安裝我的第一件事當然是跑分測試啦。 Geenbench iMac Pro (2017, 18 cores, 64G):5193 (single), 46001 (multi) iMac 5K (2014, i5 4 cores, 16G): 4245, 12645
Mar 29, 2018 — 6 min read
自組一台黑蘋果 Mac Pro

自組一台黑蘋果 Mac Pro

我的 iMac 是 2014 年 5k iMac,雖然還能動,但用 Xcode 還是常常彩虹球。其實一直也想自組一台黑蘋果,目標是 8 Core 以上的工作站。經過上次的組裝後自信可以自行完成,可以計劃一下了。 為何不買 iMac 、 iMac Pro或未來的組合式 Mac Pro? 單以價錢而論,如果買一個 5K 顯示器再組一台 PC 其實不會便宜過買 iMac。但是因為是一體式電腦,想要更新的話必需整台機器換掉,這個華麗的 5K 顯示器和內部如底板基本上就變垃圾了 (新的 iMac 沒有 target display mode,就算有當年也沒有方法輸出 5K 解像度...)。 再說想要高效能就必需買 iMac Pro,那個超高價格換來是超高的規格:32G
Mar 16, 2018 — 4 min read
reality © 2022
  • Sign up
Powered by Ghost