Joshua Muheim
2009-Apr-19 21:51 UTC
cycle(...) - is there some sort of only_first_time(...)?
Hi all The cycle() method is very handy in views. But is there some sort of only_first_time() that returns the passed value only the first time it is invoked within a view? Thanks for help :-) Josh -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2009-Apr-19 22:38 UTC
Re: cycle(...) - is there some sort of only_first_time(...)?
On Apr 19, 2009, at 5:51 PM, Joshua Muheim wrote:> > Hi all > > The cycle() method is very handy in views. But is there some sort of > only_first_time() that returns the passed value only the first time it > is invoked within a view? > > Thanks for help :-) > Josh > --Just write one: in a helper: def only_first_time(what) first_time = @previous_what.nil? @previous_what ||= what what if first_time end in a view: <ol> <% 5.times do |i| -%> <li>say <%= only_first_time(''once'') %></li> <% end -%> </ol> the resulting HTML: <ol> <li>say once</li> <li>say </li> <li>say </li> <li>say </li> <li>say </li> </ol> -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Muheim
2009-Apr-19 22:52 UTC
Re: cycle(...) - is there some sort of only_first_time(...)?
> Just write one: > > in a helper: > def only_first_time(what) > first_time = @previous_what.nil? > @previous_what ||= what > what if first_time > endThanks, but that''s not very versatile. I could only use it once. AFAIK I can use cycle() wherever I want, so I''d like the helper to be somehow dependant from where it has been called. Is this possible? Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Waldron
2009-Apr-20 00:27 UTC
Re: cycle(...) - is there some sort of only_first_time(...)?
It might be instructive to look at the code for the cycle method. It
actually creates an object and calls its to_s method each time you call
''cycle''.
You could do a similar thing. Create a small class with a
''to_s'' method
that returns something the first time, but nothing thereafter.
class Once
def initialize(first_value)
@value = first_value
end
def reset
@value = ''''
end
def to_s
value = @value
reset
return value
end
end
Stick that in lib/ or include it some other way. Then you can call cycle,
but pass your object as a single argument to cycle (cycle doesn''t
require
actually more than one param, though you could pass your object as both
params, or just pass it as the first param and '''' as the
second):
<% label_once = Once.new(''_bar'') %>
<tr class=''foo<%= cycle(label_once) %>''>
You''ll get
<tr class=''foo_bar''>
...
<tr class=''foo''>
...
<tr class=''foo''>
Might be a bit overkill, but kind of fun. :)
On Sun, Apr 19, 2009 at 5:52 PM, Joshua Muheim <
rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
>
> > Just write one:
> >
> > in a helper:
> > def only_first_time(what)
> > first_time = @previous_what.nil?
> > @previous_what ||= what
> > what if first_time
> > end
>
> Thanks, but that''s not very versatile. I could only use it once.
AFAIK I
> can use cycle() wherever I want, so I''d like the helper to be
somehow
> dependant from where it has been called. Is this possible?
>
> Thanks
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Jason Arora
2009-Apr-20 19:03 UTC
Re: cycle(...) - is there some sort of only_first_time(...)?
I would prefer to use each_with_index:
@records.each_with_index do |r, i|
"first record: #{r.name}" if i == 0
"just another record: #{r.name}"
end
On Apr 19, 5:27 pm, Ryan Waldron
<r...-L2nCScK6t0HQT0dZR+AlfA@public.gmane.org>
wrote:> It might be instructive to look at the code for the cycle method. It
> actually creates an object and calls its to_s method each time you call
> ''cycle''.
>
> You could do a similar thing. Create a small class with a
''to_s'' method
> that returns something the first time, but nothing thereafter.
>
> class Once
> def initialize(first_value)
> @value = first_value
> end
>
> def reset
> @value = ''''
> end
>
> def to_s
> value = @value
> reset
> return value
> end
> end
>
> Stick that in lib/ or include it some other way. Then you can call cycle,
> but pass your object as a single argument to cycle (cycle doesn''t
require
> actually more than one param, though you could pass your object as both
> params, or just pass it as the first param and '''' as the
second):
>
> <% label_once = Once.new(''_bar'') %>
>
> <tr class=''foo<%= cycle(label_once) %>''>
>
> You''ll get
>
> <tr class=''foo_bar''>
> ...
> <tr class=''foo''>
> ...
> <tr class=''foo''>
>
> Might be a bit overkill, but kind of fun. :)
>
> On Sun, Apr 19, 2009 at 5:52 PM, Joshua Muheim <
>
> rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:
>
> > > Just write one:
>
> > > in a helper:
> > > def only_first_time(what)
> > > first_time = @previous_what.nil?
> > > @previous_what ||= what
> > > what if first_time
> > > end
>
> > Thanks, but that''s not very versatile. I could only use it
once. AFAIK I
> > can use cycle() wherever I want, so I''d like the helper to be
somehow
> > dependant from where it has been called. Is this possible?
>
> > Thanks
> > --
> > Posted viahttp://www.ruby-forum.com/.