Three words: “Ruby on iPhone”

Yes, that’s right, ladies and gents. Thanks the hackery of a great many individuals (no, I am not among them), just this evening, I ran the following on my iPhone:

ruby -e “10.times { puts ‘foo’ }”

You can guess what happened.

Here’s how you do it, in short order, with minimal agony: Use the unofficial Installer.app developed by some folks at NullRiver. That’s it! From there, install the “Community Sources”, install “Ruby”, the BSD extensions, and “Mobile Terminal” and then go rubify yourself.

Ironically, this is not officially a product of theirs — more than likely because it is unwise to declare third party software for the iPhone as an official product at this tenuous time.

I wonder how long it will be before some intrepid Ruby coder works out a way to get to UIKit easily from Ruby and gens up some rdoc for it. I wonder if SWIG would help here?

Just for giggles, I tried:

ruby -e ’start=Time.now;a=1;b=1;1000000.times {a=a+b};puts a;puts “time: #{Time.now - start}”‘”

On my 2.16 Ghz Core Duo Macbook Pro, this took 0.280763 seconds. On the iPhone, this took 10.060393 seconds.

Looks like a speed difference of a factor of 50 for this simple computation.

Update: Um, ok, maybe it’s closer to 36 than 50.

17 comments ↓

#1 Robert on 08.20.07 at 2:52 am

Isn’t that more like a factor of ~35? You make the iPhone worse than it is. :-)

#2 Evan on 08.20.07 at 6:21 am

Doh! Sure is. That’s what I get for doing a super-quick calculation in my head before hitting the sack!

#3 court3nay on 08.28.07 at 11:41 pm

try running “irb”, “erb”, or something simple like ruby -e ‘require “thread”‘

it segfaults or dies with BUG.

#4 dan on 09.09.07 at 12:33 pm

i can’t get irb to work, either, but i have been able to write and execute basic ruby programs

#5 Kent on 09.09.07 at 9:42 pm

I followed your direction exactly, but keep getting “zsh: command not found: ruby”

I am working out of /usr/local/bin

Any advice you can offer is greatly appreciated.

#6 Evan on 09.09.07 at 9:57 pm

Kent: I should have said “./ruby”. Remember that /usr/local/bin won’t be in your PATH unless you explicitly put it there first.

#7 Kent on 09.09.07 at 10:02 pm

Can you tell I don’t do much from the command line? Thank you very much! Works like a charm.

#8 Kent on 09.09.07 at 10:33 pm

Sorry to ask such a stupid question, but how can I permanently add this to the PATH? I modified my profile and it doesn’t seem to do the trick.

#9 Evan on 09.09.07 at 11:06 pm

Kent: Not stupid at all. Now let me see if I can answer your question seeing as how I’ve never used ZSH ;).

At least you supplied the first clue with the error message; I didn’t know that the shell was using ZSH!

A quick google turned up the ZSH FAQ here: http://www.faqs.org/faqs/unix-faq/shell/zsh/. I believe that section “3.2: In which startup file do I put…?” should answer the bulk of your question. I only very quickly perused the FAQ but here’s what I believe that you will need:

Create a file in / called “.zlogin”. Inside .zlogin, you will want one line that reads “export PATH=$PATH:/usr/local/bin”.

Here’s the catch: I haven’t figured out how to exit from VIM using the iPhone keyboard. Hell, I’m not sure that you can yet ;). As such, you will likely want to write your .zlogin file on your Mac (you are using a Mac, right? ;)) and copy it to the iPhone using iPHUC.

Happy hunting.

#10 Kent on 09.10.07 at 1:12 am

I created a .zlogin file right on the iPhone using MobileTextEdit. No luck.

Thanks for the help tho. Much appreciated.

#11 Evan on 09.10.07 at 6:05 am

FWIW, make sure that you put the file in the directory that is listed if you type “echo $HOME” on the command line. If that doesn’t work, then my bag o’ tricks is not looking so good. Perhaps “.zshrc” instead of “.zlogin”?

#12 Ken Rimple on 09.16.07 at 2:08 pm

Kent,

I use .profile instead of .login or .zlogin:

Also, the nullriver installer seems to have moved everything to /usr/iphone/bin

In my .profile:

PATH=$PATH:/opt/iphone/bin export PATH alias ll=’ls -l’

#13 Ken Rimple on 09.16.07 at 2:09 pm

Sorry that was /opt/iphone/bin…

Ken

#14 dan on 09.23.07 at 12:35 pm

I’ve been trying to modify the $PATH using pico on the iphone, but I can’t figure out how to save. Anyone? Also, anyone seen irb working on an iphone yet?

#15 g-man on 11.06.07 at 6:40 am

Thanks Ken, works fine.

cd ~

echo “PATH=$PATH:/opt/iphone/bin”>>.profile

(reboot) (ruby away)

#16 louis on 11.08.07 at 3:10 pm

since there’s just one user on the iphone (root), i just added /opt/iphone/bin to /etc/profile. quit the terminal and try again. type ‘echo $PATH’ to see if it shows up.

#17 denisboy on 11.24.07 at 8:48 pm

Sorry for asking an inexperienced question but I really want to run Ruby on my iPhone.

How do you create the “.profile” file? Like can you just use your computer notepad and save it with a special extension (.exe;.app;.rb…etc) or do you have to create it on the iPhone itself? Also what commands do you enter to copy the file to the iPhone in iPhuc because I typed in:

‘putfile .profile at /’

But it didn’t show up on my iPhone when I looked for it with squid and finder. Please help!!!

Leave a Comment