LoginSignup
18
7

More than 5 years have passed since last update.

iOSで動くRubyについて

Posted at

普段はCRubyやJRubyを使っている人が多いと思いますが、今日は私が開発しているiOSで動くRubyを紹介します。

RubyPico

ファイルビューワ、エディタ、実行環境を含みます。(無料です)

App Storeからダウンロードできます。

irb

スマホからirbが使えます。Rubyの文法をちょっと確認したりするときに便利です。

rubypico-advent-01.png

irb自体もRubyPicoを使って書かれています。見慣れたRubyのコードに近くないですか?

# # irb
#
# ## Description
# Interactive Ruby Shell (REPL).

puts "irb - Interactive Ruby Shell"
no = 0

loop do
  print "irb:%03d> " % no
  cmd = gets

  puts cmd

  break if cmd == "exit"

  begin
    puts "=> #{eval(cmd).inspect}"
  rescue Exception => e
    puts e.message
  end

  no += 1
end

LINE Notify

POSTメソッドが使えるのでLINE Notifyが送れます。GETメソッドを使いたいときはBrowser.getです。

TOKEN = "XXXXXX"

def post(message)
  Browser.post(
    "https://notify-api.line.me/api/notify",
    header: { "Authorization" => "Bearer #{TOKEN}" },
    body: { message: message }
  )
end

if $0 == __FILE__
  loop do
    post prompt
  end
end

Gistに投稿

Browser.postを使ってGistに投稿もできます。

# https://developer.github.com/v3/gists/#create-a-gist
json = {
  description: "Created by RubyPico at #{Time.now}",
  public: true,
  files: {
    "file1.txt" => {
      content: "updated file contents"
    },
    "old_name.txt" => {
      filename: "new_name.txt",
      content: "modified contents"
    },
    "new_file.txt" => {
      content: "a new file\nあいうえお"
    },
  }
}

puts Browser.post(
  "https://api.github.com/gists",
  header: { "Authorization" => "token XXXXXXXX" },
  json: json
)

まとめ

普段Rubyを使っている人ならすぐに使いこなせると思います。マニュアルの整備はまだまだなので分からないことは気軽に@ongaeshiまでどうぞ。

Appタブという自作スクリプトを簡単に呼び出すための仕組みも用意されているので、簡易ランチャーやWeb APIを叩くための便利ツールとしてお使いください。

それではスマホでも楽しいプログラミングを!

18
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
7