RubyPico開発日誌4 - getsでirbを再実装

コンソールを表示しながら入力できるようになったので、irbをgetsで再実装してみた。

# # 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

実行画面。pがちゃんとCRubyと同じ挙動になったぞ万歳。(コンソールに出力しつつ、戻り値も返す)

f:id:tuto0621:20160924004944p:plain

文字に色付けたくなるね。

GitHubの気になるユーザーのレポジトリをiOSで一覧表示する

次のバージョンのRubyPico 0.9 からリンク付き文字列を生成できるようになった。いちいちURLを表示しないですむ。

puts AttrString.new("foo", link: "http://ongaeshi.me")

GitHubのレポジトリ一覧を表示してみる。

def repos(user)
  json = Browser.json("https://api.github.com/users/#{user}/repos?sort=pushed&per_page=8")

  puts "#{user}'s repos"
  json.each do |e|
    puts AttrString.new(e["name"], link:  e["html_url"])
  end
end

repos('ongaeshi')

loop do
  print "user?> "
  name = gets
  repos(name)
end

最初に自分のレポジトリを表示する。2回目以降はユーザー名を入力してその人の最近のレポジトリを見ることができる。

f:id:tuto0621:20160924143357p:plain

matzはstreemやってるとかいつでも分かるようになった。