[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01731] Re: using eRuby from inside Ruby

From: m_seki@...
Date: 2000-03-05 23:41:22 UTC
List: ruby-talk #1731
>>>>> "M" == Michael Neumann <neumann@s-direktnet.de> writes:

    M> Is it possible to use eRuby from Ruby without executing it, only through a method-call. 
    M> Could it be loaded as module?

I wrote 'Tiny eRuby - ERb'. 
ERb is an implementation of eRuby (embedded ruby). 
You can include ERb in your script.

  o ERb#new(str)	  convert eRuby -> Ruby.
  o ERb#run([binding])	  execute script, and print result.
  o ERb#result([binding]) execute script, and return result.
  o ERb#src		  converted Ruby script.

You can use it following way.

-- ERb sample script --
require 'erb/erb' # http://www2a.biglobe.ne.jp/~seki/ruby/erb-1.2.tar.gz

class App
  def initialize
    # eRuby script
    script = <<EOS
<DL>
<% hash.each { |k, v| %>
<DT><%= k %> <DD><%= v %>
<% } %>
</DL>
EOS
    # eRuby -> ERb
    @erb = ERb.new(script)
  end

  def display(hash)
    # eval
    @erb.result(binding)
  end

  def debug
    puts @erb.src
  end
end
    
app = App.new
puts "<H1>ENV</H1>"
puts app.display(ENV)
puts "<H1>Seki</H1>"
puts app.display({'given'=>'Masatoshi', 'family'=>'SEKI', 'age'=>29})

In This Thread

Prev Next