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 -}-
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.
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
} }
[…]
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.