# Cycle(...) - is there some sort of only\_first\_time(...)?

**URL:** <https://www.ruby-forum.com/t/cycle-is-there-some-sort-of-only-first-time/165926>\
**Category:** Rails\
**Created:** [April 19, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/cycle-is-there-some-sort-of-only-first-time/165926 "2009-04-19T23:51:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 19, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/cycle-is-there-some-sort-of-only-first-time/165926/1 "2009-04-19T23:51:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 12:50am UTC](https://www.ruby-forum.com/t/cycle-is-there-some-sort-of-only-first-time/165926/2 "2009-04-20T00:50:34Z")

</div>

On Apr 19, 2009, at 5:51 PM, Joshua M. 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:

 \<% 5.times do |i| -%\> 1. say \<%= only\_first\_time('once') %\>
 \<% end -%\>

the resulting HTML:

1. say once
2. say 
3. say 
4. say 
5. say 

-Rob  
Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 12:52am UTC](https://www.ruby-forum.com/t/cycle-is-there-some-sort-of-only-first-time/165926/3 "2009-04-20T00:52:56Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/cycle-is-there-some-sort-of-only-first-time/165926/4 "2009-04-20T21:04:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [April 20, 2009, 2:28am UTC](https://www.ruby-forum.com/t/cycle-is-there-some-sort-of-only-first-time/165926/5 "2009-04-20T02:28:48Z")

</div>

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’) %\>

You’ll get

 ... ... 

Might be a bit overkill, but kind of fun. 🙂

On Sun, Apr 19, 2009 at 5:52 PM, Joshua M. \<
