AWDWR question

In an early section on Action View, showing code being put directly
into the rhtml file:

<% require ‘date’
DAY_NAMES = %w{ Sunday Monday Tuesday Wednesday Thursday Friday Saturday
}
today = Date.today %>

Hello, Dave

It's <%= DAY_NAMES[today.wday] %>. Tomorrow is <%= DAY_NAMES[(today + 1).wday %>.

I get this error -

compile error
./script/…/config/…/app/views/signup/registration.rhtml:2: dynamic
constant assignment
DAY_NAMES = %w{ Sunday Monday Tuesday Wednesday Thursday Friday Saturday
}
^
./script/…/config/…/app/views/signup/registration.rhtml:8: parse
error, unexpected ‘)’, expecting ‘]’
_erbout.concat "Tomorrow is "; _erbout.concat(( DAY_NAMES[(today +
1).wday ).to_s); _erbout.concat “.\n”

Something wrong with declaring the constant ?

Stuart

Hi –

On Sat, 15 Jul 2006, Dark A. wrote:

Tomorrow is <%= DAY_NAMES[(today + 1).wday %>.
You’re missing a ] on that last line, after wday.

David


http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
[email protected] => me

Hi –

On Sat, 15 Jul 2006, Dark A. wrote:

}
David

I would have felt bad if I posted to the list for a typo but that
didn’t seem to be the issue.
It’s something related to declaring a constant. When I switchd
DAY_NAMES to day_names the code worked. What up with dat ?

I’d only tried running it through ERb with the typo fixed, which
worked – but experimenting a little more, it does indeed seem to be
true that when ActionPack does its rendering, constant assignments are
regarded as dynamic, which isn’t allowed. (“def x; A=1; end” won’t
even parse.) So this is a case where there’s a difference between
command-line erb and the erb that’s taking place programmatically in
the course of rendering.

You can, if you really want to, get around this with const_set, but
there’s probably no occasion where you’d really have a good reason to
define a constant in a view template.

David


http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack’s][ Web]log
[email protected] => me

On 7/15/06, [email protected] [email protected] wrote:

Hello, Dave

It's <%= DAY_NAMES[today.wday] %>. Tomorrow is <%= DAY_NAMES[(today + 1).wday %>.

You’re missing a ] on that last line, after wday.

David

I would have felt bad if I posted to the list for a typo but that
didn’t seem to be the issue.
It’s something related to declaring a constant. When I switchd
DAY_NAMES to day_names the code worked. What up with dat ?

Stuart

On Jul 15, 2006, at 8:43 PM, Dark A. wrote:

Right, the whole example was basically to show what not to do :slight_smile: of
course more eloquently demonstrated with purpose.
Thank you for checking and the explanation!

And I changed it to use a local variable thanks to this thread.

Dave

Right, the whole example was basically to show what not to do :slight_smile: of
course more eloquently demonstrated with purpose.
Thank you for checking and the explanation!

Stuart

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs