Skip to content

Instantly share code, notes, and snippets.

@znz
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save znz/849166c048a2117c341d to your computer and use it in GitHub Desktop.
Save znz/849166c048a2117c341d to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# env_ltsv_each.rb
# https://gist.github.com/znz/849166c048a2117c341d
# copyright (c) 2014 Kazuhiro NISHIYAMA
# This software is released under the MIT License.
# http://opensource.org/licenses/MIT
require 'ostruct'
def env_ltsv_each(prefix, start: 0, encoding: Encoding::UTF_8, scrub: false)
block_given? or return enum_for(__method__)
i = start
loop do
ltsv = ENV["#{prefix}_#{i}"]
i = i.succ
break unless ltsv
h = OpenStruct.new
ltsv.split("\t").each do |pair|
key, value = pair.split(':', 2)
value.force_encoding(encoding)
value.scrub!(scrub) if scrub
h[key] = value
end
yield h
end
nil
end
if __FILE__ == $0
ENV['NAVI_0'] = 'icon:top label:Top uri:/'
ENV['NAVI_1'] = 'icon:posts label:Listing Posts uri:/posts'
ENV['NAVI_2'] = 'icon:users label:Listing Users uri:/users'
env_ltsv_each('NAVI') do |h|
puts %Q(<a href="#{h.uri}"><img src="#{h.icon}.png" alt="#{h.label}"></a>)
#=>
# <a href="/"><img src="top.png" alt="Top"></a>
# <a href="/posts"><img src="posts.png" alt="Listing Posts"></a>
# <a href="/users"><img src="users.png" alt="Listing Users"></a>
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment