Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: Newbie question
Jamis Buck
12/3/2004 6:00:00 PM
Williams, Chris wrote:
> Hi all,
> I'm developing a rails web app here at work and I keep running into
> a common thing in terms of setting instance variables with the property of a
> form's parameter value. Basically I want the variable to take the
> parameter's value if it exists, and to otherwise use a reasonable default.
>
> I keep writing code like this:
> if @params["limit"]
> @limit = @params["limit"]
> else
> @limit = 50
> end
>
> if @params["chain"]
> chain = @params["chain"]
> else
> chain = 1
> end
>
> Can someone please point me to a simpler, more succinct "ruby" way of doing
> this?
The || operator works beautifully in this kind of situation:
@limit = @params["limit"] || 50
chain = @params["chain"] || 1
>
> Thanks,
> Chris
>
- Jamis
--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Newbie question
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password