shin
shin

Reputation: 32721

Difference between ERB#result and ERB#run

This is erb3.rb

require 'erb'

weekday = Time.now.strftime('%A')
simple_template = "Today is <%= weekday %>."

renderer = ERB.new(simple_template)
puts renderer.result
renderer.run

When I run this I get the followings.

➜ ruby erb3.rb
Today is Friday.
Today is Friday.%

Q1. I understand that I don't need to write puts renderer.run. But are there any more differences?

Q2. The output of put renderer.run is Today is Friday.%. What is % at the end. When I use puts renderer.run then it doesn't output %.

Upvotes: 2

Views: 85

Answers (2)

Josh
Josh

Reputation: 5721

Regarding Q1, there is no difference between writing puts renderer.result or renderer.run. UPDATE: Thank you @muistooshort for pointing out the source for run:

def run(b=new_toplevel)
  print self.result(b)
end

As you can see, it is simply printing the output of result.


Regarding Q2, I believe the % just indicates a new line. If you change your script to the following it goes away.

renderer = ERB.new(simple_template)
puts renderer.result
renderer.run
puts ""
#>Today is Thursday.
#>Today is Thursday.

Upvotes: 2

mechanicalfish
mechanicalfish

Reputation: 12826

renderer.run prints the result meaning that there is no new newline after the output which is indicated by the % (I guess because you are using zsh). You can get the same result like this:

➜ puts 'a'; print 'a'
a
a%

Upvotes: 0

Related Questions