# \[ruby\] c#-like syntax and others

**URL:** <https://www.ruby-forum.com/t/ruby-c-like-syntax-and-others/78925>\
**Category:** Rails\
**Created:** [December 10, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/ruby-c-like-syntax-and-others/78925 "2006-12-10T18:32:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71e660/32.png) [@George](https://www.ruby-forum.com/u/George)\
**Post date:** [December 10, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/ruby-c-like-syntax-and-others/78925/1 "2006-12-10T18:32:49Z")

</div>

If I’m not mistaken I’ve read once about ruby’s flexibility in letting  
you do something your own way (there is always more than 1 way to do  
something and ruby lets you choose your own way).  
And yes, syntax in ruby is flexible. But there is a way to write ruby  
code in c# style?  
writing  
1.upto( 2 )  
{ | i |  
puts i.to\_s  
}  
throws error.

you have to write  
1.upto( 2 ){ | i | # just one line here  
puts i.to\_s  
}  
to work  
It seems here isn’t your way. it’s just ruby way…  
Or is there way I dont know to write c#-like code:  
function/instruction  
begin sign -{-  
… block’s code …  
end sign -}-

---

<div class="post-metadata">

**Author:** ![George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71e660/32.png) [@George](https://www.ruby-forum.com/u/George)\
**Post date:** [December 10, 2006, 7:15pm UTC](https://www.ruby-forum.com/t/ruby-c-like-syntax-and-others/78925/2 "2006-12-10T19:15:36Z")

</div>

On Dec 10, 2006, at 12:32 PM, George wrote:

> }  
> begin sign -{-  
> … block’s code …  
> end sign -}-

For short blocks, I believe it’s idiomatic to do:

1.upto(2) { |i| puts i.to\_s }

This also works but isn’t idiomatic because it puts the ‘|i|’ on its  
own line:

1.upto(2) {  
|i|  
puts i.to\_s  
}

For longer blocks, it’s idiomatic to use do…end:

1.upto(2) do |i|  
puts i.to\_s

# more work here…

# and here…

# and so on…

end

However, if you don’t provide the opening ‘{’ or ‘do’ on the same  
line as the 1.upto(2), you’ll get an error that no block was given. I  
verified this in IRB (interactive Ruby) on Mac OS X w/ Ruby 1.8.5  
from MacPorts.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71e660/32.png) [@George](https://www.ruby-forum.com/u/George)\
**Post date:** [December 10, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/ruby-c-like-syntax-and-others/78925/3 "2006-12-10T19:30:03Z")

</div>

thanks for the reply

i thought maybe there a way to tell ruby interpreter the new line in  
fact is not meant to be taken as a new line and that it’s just for code  
layout

1.upto( 3 )[a sign for ‘hey, ruby, it’s a dummy new line here’]  
{ | i |  
puts i  
}

Just like there is a sign (–\> ; \<–) to tell ruby there should be a new  
line

1.upto( 3 ){ | i | print 'current: '; puts i }

the code above works fine because there is a --\> ; \<–; otherwise we get  
an error

Craig D. wrote:

> On Dec 10, 2006, at 12:32 PM, George wrote:

---

<div class="post-metadata">

**Author:** ![George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71e660/32.png) [@George](https://www.ruby-forum.com/u/George)\
**Post date:** [December 10, 2006, 7:51pm UTC](https://www.ruby-forum.com/t/ruby-c-like-syntax-and-others/78925/4 "2006-12-10T19:51:40Z")

</div>

On Sun, Dec 10, 2006 at 07:30:03PM +0100, George wrote:  
}  
} thanks for the reply  
}  
} i thought maybe there a way to tell ruby interpreter the new line in  
} fact is not meant to be taken as a new line and that it’s just for  
code  
} layout  
}  
} 1.upto( 3 )[a sign for ‘hey, ruby, it’s a dummy new line here’]  
} { | i |  
} puts i  
} }  
[…]

1.upto( 3 )   
{ | i |  
puts i  
}

–Greg

---

<div class="post-metadata">

**Author:** ![George](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71e660/32.png) [@George](https://www.ruby-forum.com/u/George)\
**Post date:** [December 10, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/ruby-c-like-syntax-and-others/78925/5 "2006-12-10T20:54:51Z")

</div>

thank you \ 🙂  
Greg

and because there are so many ways to write ruby, is there a obfuscator  
to make the code more easy to be executed by ruby interpreter (that,  
let’s say, will transform the code from the most readible for you form  
to the easiest interpretable form for ruby) (or is it a practice in ruby  
world to obfuscate code before moving in production, and if yes, is  
there a significant performance gain)?

Gregory S. wrote:

> On Sun, Dec 10, 2006 at 07:30:03PM +0100, George wrote:  
> }  
> } thanks for the reply  
> }  
> } i thought maybe there a way to tell ruby interpreter the new line in  
> } fact is not meant to be taken as a new line and that it’s just for  
> code  
> } layout  
> }  
> } 1.upto( 3 )[a sign for ‘hey, ruby, it’s a dummy new line here’]  
> } { | i |  
> } puts i  
> } }  
> […]
> 
> 1.upto( 3 )   
> { | i |  
> puts i  
> }
> 
> –Greg
