[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

can this be more succinct?

eggie5

8/28/2007 1:59:00 PM

How can this be more succinct? Can I somehow put the nil check inline
with the each?

unless(params[:categories].nil?)
params[:categories].each do |id|
@subscriber.subscriptions.create :category_id =>
id, :timestamp_start => Time.now.to_s, :is_subscribed => true
end
end

8 Answers

Daniel Lucraft

8/28/2007 2:07:00 PM

0

eggie5 wrote:
> How can this be more succinct? Can I somehow put the nil check inline
> with the each?
>
> unless(params[:categories].nil?)
> params[:categories].each do |id|
> @subscriber.subscriptions.create :category_id =>
> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
> end
> end


(params[:categories]||[]).each do |id|
@subscriber.subscriptions.create :category_id => id,
:timestamp_start => Time.now.to_s, :is_subscribed => true
end

best,
Dan
--
Posted via http://www.ruby-....

Bertram Scharpf

8/28/2007 2:09:00 PM

0

Hi,

Am Dienstag, 28. Aug 2007, 23:00:08 +0900 schrieb eggie5:
> How can this be more succinct? Can I somehow put the nil check inline
> with the each?
>
> unless(params[:categories].nil?)
> params[:categories].each do |id|
> @subscriber.subscriptions.create :category_id =>
> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
> end
> end

Maybe:

pc = params[:categories]
pc and pc.each do |id|
...
end

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Marcin Mielzynski

8/28/2007 2:32:00 PM

0

eggie5 wrote:
> How can this be more succinct? Can I somehow put the nil check inline
> with the each?
>
> unless(params[:categories].nil?)
> params[:categories].each do |id|
> @subscriber.subscriptions.create :category_id =>
> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
> end
> end
>

params[:categories].to_a.each do ...


lopex

Florian Aßmann

8/28/2007 4:43:00 PM

0

Hi eggie5,

> How can this be more succinct? Can I somehow put the nil check inline
> with the each?
>
> unless(params[:categories].nil?)
> params[:categories].each do |id|
> @subscriber.subscriptions.create :category_id =>
> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
> end
> end

Just a minor thingy, rename timestamp_start column to created_at and
Rails does its magic. So you can write (categories replaced with
category_ids!):

(params[:category_ids] || []).each do |category_id|
@subscriber.subscriptions.create(:category_id => category_id,
:is_subscribed => true)
end


You can also put a verify in the class definition of your controller:

class SubscriptionsController < ApplicationController
verify :params => :category_ids, :method => :post,
:only => :subscribe,
:redirect_to => :back

def subscribe
# ...
# both String and Array do have the each method...
params[:category_ids].each do |category_id|
@subscriber.subscriptions.create(:category_id => category_id,
:is_subscribed => true)
end
# ...
end

# ...
end


You can even move this whole bunch of code to the subscribers model itself:

class Subscriber < ActiveRecord::Base

# This methods accepts multiple category ids in form of:
# instance.subscribe 1, 2, 4, ...
def subscribe(*category_ids)
# wrap insertion of multiple records in a transactions is generally
# a good idea...
ActiveRecord::Base.transaction do
category_ids.each do |category_id|
subscriptions.create(:category_id => category_id,
:is_subscribed => true)
end
end
end

end

class SubscriptionsController < ApplicationController
verify :params => :category_ids, :method => :post,
:only => :subscribe,
:redirect_to => :back

def subscribe
# ...
@subscriber.subscribe(*params[:category_ids])
# ...
end

# ...
end


Or extend the association with a module, see AssociationExtensions in
ActiveRecord::Associations::ClassMethods. in this case you could write
something like:

instance.subscriptions.category_ids = category_ids

This encapsulate all methods in a module that are only important for the
association itself - it's clean!


If you don't use ActionPack and ActiveRecord at all don't mind this post...

Regards
Florian

P.S.
Beware, this code was not run through any functional or unit tests...

Robert Klemme

8/28/2007 5:31:00 PM

0

On 28.08.2007 16:09, Bertram Scharpf wrote:
> Hi,
>
> Am Dienstag, 28. Aug 2007, 23:00:08 +0900 schrieb eggie5:
>> How can this be more succinct? Can I somehow put the nil check inline
>> with the each?
>>
>> unless(params[:categories].nil?)
>> params[:categories].each do |id|
>> @subscriber.subscriptions.create :category_id =>
>> id, :timestamp_start => Time.now.to_s, :is_subscribed => true
>> end
>> end
>
> Maybe:
>
> pc = params[:categories]
> pc and pc.each do |id|
> ...
> end

+1

robert

vl

2/18/2011 10:49:00 PM

0


Welfare support are not forever...after 6 months then no fun,

So, it does not matter how to make a lot money as long IRS do not know
how and where......The game are break the 'systems' and retired, life
is too short to struggle......

++++++++


On Fri, 18 Feb 2011 13:43:31 -0800 (PST), Le Boomerang
<khuctinhnhan@yahoo.com> wrote:

>Welfare systems are for, use it and enjoy it, nothing wrong, no shame
>and relax with more glorious
>
>On Feb 18, 1:38 pm, vl <v...@cool.uk.org> wrote:
>.> Spent a fortune in education (tuition fees, etc...), graduated from
>> university no job and in debts, bad news, no future, something wrong
>> with capitalist system.....lost generation with wrong timing.....
>>
>> +++++++++
>>
>> On Fri, 18 Feb 2011 12:49:41 -0800 (PST), nttn <nttn1...@yahoo.com>
>> wrote:
>>
>>
>>
>>
>>
>>
>>
>> >may ma` ca'i job cu?a Nga kho^ng co' trong ca'i list na`y ... phew!!!
>>
>> >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> >http://finance.yahoo.com/career-work/article/111881/careers......
>>
>> >Top Dead or Dying Career Paths
>>
>> >Computer Operators
>>
>> >This field declined by 31% from 2004 to 2009 and lost 42,000 workers,
>> >and the BLS expects continued deterioration. With most professionals
>> >using personal computers with ever more sophisticated data processing,
>> >jobs based solely on entering commands and monitoring computer
>> >terminals are outdated or increasingly specialized.
>>
>> >Stage Performers
>>
>> >The five-year decline for this career path, which includes magicians,
>> >jugglers, clowns and dancers, was a startling 61% -- one of the
>> >steepest on this list. According to jobs researcher Laurence Shatkin,
>> >Ph.D., live performances have fallen out of fashion and have been
>> >almost entirely replaced with movies and home entertainment
>> >technologies.
>>
>> >Postal Service Mail Sorters
>>
>> >After losing almost 57,000 jobs between 2004 and 2009, the BLS expects
>> >a further 30% decline in this occupation by 2018. With more automated
>> >processes for mail sorting and increasing correspondence via e-mail
>> >and fax, this job is quickly becoming unnecessary.
>>
>> >Holistic Healers
>>
>> >Alternative medicine specialists like acupuncturists, homeopathic
>> >doctors and hypnotherapists may be an endangered species. The field
>> >declined 44% between 2004 and 2009, losing about 26,000 jobs. Because
>> >health insurance companies typically do not cover these specialties,
>> >alternative medicine may be becoming a more niche, luxury service.
>>
>> >Office and Administrative Support Workers
>>
>> >About 300,000 administrative jobs disappeared between 2004 and 2009,
>> >and the BLS projects continued contraction throughout the next decade.
>> >Secretaries and file clerks are no longer in demand as companies cut
>> >costs. Moreover, technologies like voicemail and easy-to-use word
>> >processors have enabled professionals to do their own clerical work.

:))

2/18/2011 10:57:00 PM

0

On Fri, 18 Feb 2011 22:48:45 +0000, vl <vl@cool.uk.org> wrote:

>
>Welfare support are not forever...after 6 months then no fun,
>
>So, it does not matter how to make a lot money as long IRS do not know
>how and where......The game are break the 'systems' and retired, life
>is too short to struggle......
>
>++++++++
>
-----------------------------------------------------------------------------------------------------------------------

;-)))

Cock Your Head

http://jupiterean.blogspot.com/2008_08_01_ar...

There was a ding, the elevator door opened. A woman stepped out,
followed by a small boy of maybe three or four years old. The boy was
speaking with excitement, ?Mom, Mom, it?s a vee!? to which his mother
replied, ?No, it?s an el.? I could still hear him protest that it was
a vee and his mother firmly assert that it was an el on their way out
of the building.

The overheard conversation intrigued me, and when inside the elevator
pressing on the button to my floor I suddenly understood what the boy
and his mother had talked about. On the control panel there was a
button with the letter L for Lobby, and the boy must have cocked his
head to see a V instead.

Smart little fellow, I thought and couldn?t suppress a grin. Too bad
his mother didn?t see it that way, but that?s exactly how a virgin
mind was stripped of its originality and molded into seeing things the
way everyone else did. Too bad we reduce ourselves to a bunch of
horses with blinkers.

How about this for a change. When you get stuck or simply get bored,
just cock your head when looking at whatever interests you. Like that
boy, you might see a V instead of an L, and that might give you a
whole new set of eyes to see the world with.


>
>On Fri, 18 Feb 2011 13:43:31 -0800 (PST), Le Boomerang
><khuctinhnhan@yahoo.com> wrote:
>
>>Welfare systems are for, use it and enjoy it, nothing wrong, no shame
>>and relax with more glorious
>>
>>On Feb 18, 1:38 pm, vl <v...@cool.uk.org> wrote:
>>.> Spent a fortune in education (tuition fees, etc...), graduated from
>>> university no job and in debts, bad news, no future, something wrong
>>> with capitalist system.....lost generation with wrong timing.....
>>>
>>> +++++++++
>>>
>>> On Fri, 18 Feb 2011 12:49:41 -0800 (PST), nttn <nttn1...@yahoo.com>
>>> wrote:
>>>
-------------------------------------------------------------------------------------------------------------------------------------------
>>>
>>> >may ma` ca'i job cu?a Nga kho^ng co' trong ca'i list na`y ... phew!!!
>>>
>>> >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>
>>> >http://finance.yahoo.com/career-work/article/111881/careers......
>>>
>>> >Top Dead or Dying Career Paths
>>>
>>> >Computer Operators
>>>
>>> >This field declined by 31% from 2004 to 2009 and lost 42,000 workers,
>>> >and the BLS expects continued deterioration. With most professionals
>>> >using personal computers with ever more sophisticated data processing,
>>> >jobs based solely on entering commands and monitoring computer
>>> >terminals are outdated or increasingly specialized.
>>>
>>> >Stage Performers
>>>
>>> >The five-year decline for this career path, which includes magicians,
>>> >jugglers, clowns and dancers, was a startling 61% -- one of the
>>> >steepest on this list. According to jobs researcher Laurence Shatkin,
>>> >Ph.D., live performances have fallen out of fashion and have been
>>> >almost entirely replaced with movies and home entertainment
>>> >technologies.
>>>
>>> >Postal Service Mail Sorters
>>>
>>> >After losing almost 57,000 jobs between 2004 and 2009, the BLS expects
>>> >a further 30% decline in this occupation by 2018. With more automated
>>> >processes for mail sorting and increasing correspondence via e-mail
>>> >and fax, this job is quickly becoming unnecessary.
>>>
>>> >Holistic Healers
>>>
>>> >Alternative medicine specialists like acupuncturists, homeopathic
>>> >doctors and hypnotherapists may be an endangered species. The field
>>> >declined 44% between 2004 and 2009, losing about 26,000 jobs. Because
>>> >health insurance companies typically do not cover these specialties,
>>> >alternative medicine may be becoming a more niche, luxury service.
>>>
>>> >Office and Administrative Support Workers
>>>
>>> >About 300,000 administrative jobs disappeared between 2004 and 2009,
>>> >and the BLS projects continued contraction throughout the next decade.
>>> >Secretaries and file clerks are no longer in demand as companies cut
>>> >costs. Moreover, technologies like voicemail and easy-to-use word
>>> >processors have enabled professionals to do their own clerical work.

vl

2/19/2011 2:05:00 PM

0

On Fri, 18 Feb 2011 14:56:35 -0800, ":))" <bennypooh0@yahoo.com>
wrote:

>On Fri, 18 Feb 2011 22:48:45 +0000, vl <vl@cool.uk.org> wrote:
>
>>
>>Welfare support are not forever...after 6 months then no fun,
>>
>>So, it does not matter how to make a lot money as long IRS do not know
>>how and where......The game are break the 'systems' and retired, life
>>is too short to struggle......
>>
>>++++++++
>>
>-----------------------------------------------------------------------------------------------------------------------
>
>;-)))

VNmese cocking up the head are irrelevant in the real world of USA and
EU...

Here is a better story...

There are 2 types of elevator...

In elevator No.1: there are pimps and prostitues who were lucky
getting out of salty sea water and volonteering as foot soldiers for
dirty money in down town LA / NY and other cities etc.. so that their
sons and daughters can get proper education than the good old Sth VN
education system...Tuition fees and pockets money are no problems for
thieves and pimps and dealers and prostitues...(nga ba chu ia..)

Pimps and thieves and VNmese prostitues can be found in LA / Sunset
Boulevard.

In elevator No.2 : there were hard working VNmese people who get rip
off by VNmese pimps, thieves and prostitues so that their sons and
daughters can be better at bullshitting among their own kind....the
boy in elevator said to his mother that he is getting fed up with
pimps and thieves and want DEA and FBI sending bad VNmese to HCM
town...

I saw these vulgar rich slit eyes VNmese and Chinese in Europe and US
of A (NY..), they look pathetic and do not fit in with normal VNmese /
Chinese communities....They walked around showing off money like
pathetic thieves and of scums......


+++++++++++++






>
>Cock Your Head
>
>http://jupiterean.blogspot.com/2008_08_01_ar...
>
>There was a ding, the elevator door opened. A woman stepped out,
>followed by a small boy of maybe three or four years old. The boy was
>speaking with excitement, ?Mom, Mom, it?s a vee!? to which his mother
>replied, ?No, it?s an el.? I could still hear him protest that it was
>a vee and his mother firmly assert that it was an el on their way out
>of the building.
>
>The overheard conversation intrigued me, and when inside the elevator
>pressing on the button to my floor I suddenly understood what the boy
>and his mother had talked about. On the control panel there was a
>button with the letter L for Lobby, and the boy must have cocked his
>head to see a V instead.
>
>Smart little fellow, I thought and couldn?t suppress a grin. Too bad
>his mother didn?t see it that way, but that?s exactly how a virgin
>mind was stripped of its originality and molded into seeing things the
>way everyone else did. Too bad we reduce ourselves to a bunch of
>horses with blinkers.
>
>How about this for a change. When you get stuck or simply get bored,
>just cock your head when looking at whatever interests you. Like that
>boy, you might see a V instead of an L, and that might give you a
>whole new set of eyes to see the world with.
>
>
>>
>>On Fri, 18 Feb 2011 13:43:31 -0800 (PST), Le Boomerang
>><khuctinhnhan@yahoo.com> wrote:
>>
>>>Welfare systems are for, use it and enjoy it, nothing wrong, no shame
>>>and relax with more glorious
>>>
>>>On Feb 18, 1:38 pm, vl <v...@cool.uk.org> wrote:
>>>.> Spent a fortune in education (tuition fees, etc...), graduated from
>>>> university no job and in debts, bad news, no future, something wrong
>>>> with capitalist system.....lost generation with wrong timing.....
>>>>
>>>> +++++++++
>>>>
>>>> On Fri, 18 Feb 2011 12:49:41 -0800 (PST), nttn <nttn1...@yahoo.com>
>>>> wrote:
>>>>
>-------------------------------------------------------------------------------------------------------------------------------------------
>>>>
>>>> >may ma` ca'i job cu?a Nga kho^ng co' trong ca'i list na`y ... phew!!!
>>>>
>>>> >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>>
>>>> >http://finance.yahoo.com/career-work/article/111881/careers......
>>>>
>>>> >Top Dead or Dying Career Paths
>>>>
>>>> >Computer Operators
>>>>
>>>> >This field declined by 31% from 2004 to 2009 and lost 42,000 workers,
>>>> >and the BLS expects continued deterioration. With most professionals
>>>> >using personal computers with ever more sophisticated data processing,
>>>> >jobs based solely on entering commands and monitoring computer
>>>> >terminals are outdated or increasingly specialized.
>>>>
>>>> >Stage Performers
>>>>
>>>> >The five-year decline for this career path, which includes magicians,
>>>> >jugglers, clowns and dancers, was a startling 61% -- one of the
>>>> >steepest on this list. According to jobs researcher Laurence Shatkin,
>>>> >Ph.D., live performances have fallen out of fashion and have been
>>>> >almost entirely replaced with movies and home entertainment
>>>> >technologies.
>>>>
>>>> >Postal Service Mail Sorters
>>>>
>>>> >After losing almost 57,000 jobs between 2004 and 2009, the BLS expects
>>>> >a further 30% decline in this occupation by 2018. With more automated
>>>> >processes for mail sorting and increasing correspondence via e-mail
>>>> >and fax, this job is quickly becoming unnecessary.
>>>>
>>>> >Holistic Healers
>>>>
>>>> >Alternative medicine specialists like acupuncturists, homeopathic
>>>> >doctors and hypnotherapists may be an endangered species. The field
>>>> >declined 44% between 2004 and 2009, losing about 26,000 jobs. Because
>>>> >health insurance companies typically do not cover these specialties,
>>>> >alternative medicine may be becoming a more niche, luxury service.
>>>>
>>>> >Office and Administrative Support Workers
>>>>
>>>> >About 300,000 administrative jobs disappeared between 2004 and 2009,
>>>> >and the BLS projects continued contraction throughout the next decade.
>>>> >Secretaries and file clerks are no longer in demand as companies cut
>>>> >costs. Moreover, technologies like voicemail and easy-to-use word
>>>> >processors have enabled professionals to do their own clerical work.