[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

syntax problem: @var=="v" ? { ... } : { ... }

Luca Scaljery

2/15/2008 9:35:00 PM

Hi All

I tried to print an "M" or "V" depending on the value of a variable,
like

@var = "v"
@var == "v" ? { p "V" } : { p "M" }

this however doesn't work, but I'm sure something like this can be done!
Any suggestions ?

Cheers
LuCa
--
Posted via http://www.ruby-....

5 Answers

Stefano Crocco

2/15/2008 9:37:00 PM

0

Alle Friday 15 February 2008, Luca Scaljery ha scritto:
> Hi All
>
> I tried to print an "M" or "V" depending on the value of a variable,
> like
>
> @var = "v"
> @var == "v" ? { p "V" } : { p "M" }
>
> this however doesn't work, but I'm sure something like this can be done!
> Any suggestions ?
>
> Cheers
> LuCa

p(@var == "v" ? "V" : "M")

Stefano


Sebastian Hungerecker

2/15/2008 9:39:00 PM

0

Luca Scaljery wrote:
> @var = "v"
> @var == "v" ? { p "V" } : { p "M" }
>
> this however doesn't work, but I'm sure something like this can be done!
> Any suggestions ?

Leave out the curlies.


--
NP: Nevermore - Believe In Nothing
Jabber: sepp2k@jabber.org
ICQ: 205544826

Stefano Crocco

2/15/2008 9:42:00 PM

0

Alle Friday 15 February 2008, Sebastian Hungerecker ha scritto:
> Luca Scaljery wrote:
> > @var = "v"
> > @var == "v" ? { p "V" } : { p "M" }
> >
> > this however doesn't work, but I'm sure something like this can be done!
> > Any suggestions ?
>
> Leave out the curlies.

And put brackets around the arguments of p, otherwise you'll get a syntax
error

Stefano


Luca Scaljery

2/15/2008 9:52:00 PM

0

thnx, that works, but what if I want to do 2 or more actions/statements
for each options ?

@var == "v" ? P("V");do_something("V") : ......

suggestions ?

Stefano Crocco wrote:
> Alle Friday 15 February 2008, Sebastian Hungerecker ha scritto:
>> Luca Scaljery wrote:
>> > @var = "v"
>> > @var == "v" ? { p "V" } : { p "M" }
>> >
>> > this however doesn't work, but I'm sure something like this can be done!
>> > Any suggestions ?
>>
>> Leave out the curlies.
>
> And put brackets around the arguments of p, otherwise you'll get a
> syntax
> error
>
> Stefano

--
Posted via http://www.ruby-....

Stefano Crocco

2/15/2008 9:56:00 PM

0

Alle Friday 15 February 2008, Luca Scaljery ha scritto:
> thnx, that works, but what if I want to do 2 or more actions/statements
> for each options ?
>
> @var == "v" ? P("V");do_something("V") : ......
>
> suggestions ?

In this case, I'd use a standard if:

if @var == "v"
p "V"
do_something
else
do_something_else
end

Stefano