Can we have these two added to ERb?

I like Ruby tremendously…

and currently having some headache with the “$” and “;” in PHP, like

$i = $j;

in every single line, vs Ruby’s simplicity

i = j

maybe the $i is so that outputting the variable is easy in

print “the value is $i”
in ERb, we need to write

“the value is #{i}”

and it is a bit more typing. can we have something like

“the value is ##i” as a shorthand? and use #{ } when it involves more
complicated expressions?

The other thing is that in PHP, they use “print” and “echo” to output
the HTML code. In Ruby, we always use <%= evaluated_value %> and it
might have a limit of always breaking up the code into <% %> and <%=
%> and <% %>

<% results.each do |k,v| %>
<%= “#{k} #{v}” %>
<% end %>

the people who like to use inject may be happy because they can have it
all in one line <%= results.inject {|k,v| k += … %>

but if we can have some way that the STDOUT is made into HTML code, like

<%p results.each {|k,v| printf "… " %>

or

<%p

 code

%>

then it can give us some ease when templating.

On Sep 28, 2007, at 09:20 , SpringFlowers AutumnMoon wrote:

maybe the $i is so that outputting the variable is easy in

print “the value is $i”
in ERb, we need to write

“the value is #{i}”

and it is a bit more typing. can we have something like

“the value is ##i” as a shorthand? and use #{ } when it involves more
complicated expressions?

There is a shortcut for ivars, cvars and globals, but I find it too
evil to write down. Especially since it can lead to mysterious
syntax errors when you’re not paying attention. I’m sure you’ll find
it soon enough.

the people who like to use inject may be happy because they can
have it
all in one line <%= results.inject {|k,v| k += … %>

but if we can have some way that the STDOUT is made into HTML code,
like

This should be easy enough to do by overwriting $stdout with an
object that writes to _erbout (or whatever it is) while in an erb
template.

On Sep 28, 10:20 am, SpringFlowers AutumnMoon
[email protected] wrote:

in ERb, we need to write

“the value is #{i}”

and it is a bit more typing. can we have something like

“the value is ##i” as a shorthand? and use #{ } when it involves more
complicated expressions?

This feature has nothing to do with ERb; it’s part of Ruby’s string
interpolation.

For global and instance variables, you can already do this:
“the value is #@i

If that’s not enough, you’ll have to make a case for an alternate
syntax for local variable interpolation.

all in one line <%= results.inject {|k,v| k += … %>

but if we can have some way that the STDOUT is made into HTML code, like

<%p results.each {|k,v| printf "… " %>

  1. ERb has a local variable that you can append to if you like.

C:>qri ERB.new

ERB::new
ERB::new(str, safe_level=nil, trim_mode=nil, eoutvar=‘_erbout’)

 Constructs a new ERB object with the template specified in _str_.

 ...

 _eoutvar_ can be used to set the name of the variable ERB will
 build up its output in. This is useful when you need to run
 multiple ERB templates through the same binding and/or when you
 want to control where output ends up. Pass the name of the

variable
to be used inside a String.

I find this useful when I want to control whitespace exactly and don’t
want to have to mess up my <%…%> delimiters to do so. For example:

<%10.times{
_erbout << “\t\tHello World!\n”
}%>

  1. After prompting for this a couple years ago, people on the list
    helped create a patch to ERB to allow print and puts to concatenate to
    this string. See [1] below. This was followed by some discussion
    (again prompted by me) for a RCR to allow this to work. See [2] below.
    The discussion there is probably worth reading before we dive into
    this again.

[1]
http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/155537?155418-156804+split-mode-vertical

[2]
http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/152709?152641-153992+split-mode-vertical

On Sep 28, 10:41 am, Phrogz [email protected] wrote:

  1. After prompting for this a couple years ago, people on the list
    helped create a patch to ERB to allow print and puts to concatenate to
    this string. […]

I forgot to mention - this patch is available for Ruby 1.8.2 at:
ftp://phrogz.net/rubylibs/erb_1.8.2_with_puts.rb.gz

I’m not sure how much (if any) ERb has changed between 1.8.2 and
1.8.6, but it should be pretty easy to carry this patch forward to the
current version and patch your own ERb if you want this.

Gavin K. wrote:

On Sep 28, 10:20 am, SpringFlowers AutumnMoon
[email protected] wrote:

in ERb, we need to write

“the value is #{i}”

and it is a bit more typing. can we have something like

“the value is ##i” as a shorthand? and use #{ } when it involves more
complicated expressions?

This feature has nothing to do with ERb; it’s part of Ruby’s string
interpolation.

For global and instance variables, you can already do this:
“the value is #@i

If that’s not enough, you’ll have to make a case for an alternate
syntax for local variable interpolation.

how about the case that in RoR or server side scripting usage, it can be
quite simpler, to contend for Ruby as a server side scripting language?

Eric H. wrote:

This should be easy enough to do by overwriting $stdout with an
object that writes to _erbout (or whatever it is) while in an erb
template.

hm, that’s interesting, the program “erb” can already take

puts “string”

and output it…

but in a somewhat funny order:

<%= “haha” %>

<% puts “so how about this?” %>

C:\rails\depot>erb try_erb.txt
so how about this?

haha

However, when I use puts “string” in index.rhtml on RoR, it won’t get
displayed. So RoR actually uses eRuby but not erb, and both are just
called “Embedded Ruby” as “ERb”?

So my original question becomes, can we have those two features added to
eRuby. (for RoR and for server side scripting language)

Quoth SpringFlowers AutumnMoon:

maybe the $i is so that outputting the variable is easy in

all in one line <%= results.inject {|k,v| k += … %>

%>

then it can give us some ease when templating.

PHP has <?= $var ?> shorthand as well.

On Sep 28, 10:45 am, Phrogz [email protected] wrote:

I forgot to mention - this patch is available for Ruby 1.8.2 at:
ftp://phrogz.net/rubylibs/erb_1.8.2_with_puts.rb.gz

Ack, that should be:
http://phrogz.net/rubylibs/erb_1.8.2_with_puts.rb.gz

Sorry for the extra noise.

On Sep 28, 11:29 am, SpringFlowers AutumnMoon
[email protected] wrote:

how about the case that in RoR or server side scripting usage, it can be
quite simpler, to contend for Ruby as a server side scripting language?

You don’t need to convince me; I’m a fan of the idea. You need to
convince everyone else who feels that it’s just as easy (and more
clear) to concatenate to _erbout << instead of print/puts/p.

If you read the full discussion when I proposed the RCR two years ago,
you’ll see my arguments and theirs. Don’t be disheartened, but if
you’re going to make a strong case for it effectively, you’ll want to
continue the discussion previously had, instead of starting anew.

(Saying “This feature is important because it will make Ruby more
popular in the world of ____” is nice, but it doesn’t say why it’s
important to have that feature, or why you think it will make Ruby
more popular. You need to either describe what it makes possible that
was not possible before, or convince people why it’s a much better way
to do things than the currently-possible solution.)

On Sep 28, 11:35 am, SpringFlowers AutumnMoon
[email protected] wrote:

hm, that’s interesting, the program “erb” can already take

puts “string”

and output it…

No, it can’t. The ruby program evaluates that and outputs it
separately. It is not part of the string resulting from evaluating the
ERB template.

but in a somewhat funny order:

<%= “haha” %>

<% puts “so how about this?” %>

C:\rails\depot>erb try_erb.txt
so how about this?

haha

What’s happening here is that while evaluating the ERB template, the
#puts method is called…which outputs it immediately. (And as a
result, doesn’t happen to change the ERB string at all.) Then, later,
the template finishes, and has just the contents of “haha”.

However, when I use puts “string” in index.rhtml on RoR, it won’t get
displayed. So RoR actually uses eRuby but not erb, and both are just
called “Embedded Ruby” as “ERb”?

In RoR, you’ll see the output of the puts call coming out in your
console or logfile (I think); since the output isn’t placed in the ERB
result string, however, it never gets put in the HTML that is sent to
the web browser.

On Sep 28, 12:35 pm, Konrad M. [email protected] wrote:

PHP has <?= $var ?> shorthand as well.

As does ERB.

<%= any_expression_including_a_variable %>
e.g.

<%= foo %>
<%= @foo %>
<%= $foo %>

#to_s is called on the expression before throwing it into the string.