# Syntax problem: @var=="v" ? { ... } : { ... }

**URL:** <https://www.ruby-forum.com/t/syntax-problem-var-v/129266>\
**Category:** Ruby\
**Created:** [February 15, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/syntax-problem-var-v/129266 "2008-02-15T22:34:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 15, 2008, 10:34pm UTC](https://www.ruby-forum.com/t/syntax-problem-var-v/129266/1 "2008-02-15T22:34:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 15, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/syntax-problem-var-v/129266/2 "2008-02-15T22:37:35Z")

</div>

Alle Friday 15 February 2008, Luca S. ha scritto:

> Cheers  
> LuCa

p(@var == “v” ? “V” : “M”)

Stefano

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 15, 2008, 10:39pm UTC](https://www.ruby-forum.com/t/syntax-problem-var-v/129266/3 "2008-02-15T22:39:58Z")

</div>

Luca S. 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.

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 15, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/syntax-problem-var-v/129266/4 "2008-02-15T22:42:55Z")

</div>

Alle Friday 15 February 2008, Sebastian H. ha scritto:

> Luca S. 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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 15, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/syntax-problem-var-v/129266/5 "2008-02-15T22:51:40Z")

</div>

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 C. wrote:

> Alle Friday 15 February 2008, Sebastian H. ha scritto:
> 
> > Luca S. 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

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [February 15, 2008, 10:56pm UTC](https://www.ruby-forum.com/t/syntax-problem-var-v/129266/6 "2008-02-15T22:56:28Z")

</div>

Alle Friday 15 February 2008, Luca S. 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
