Hi
I am C++ programmer and in C++ I used some thing that I want
corresponding those things in Ruby.
please help me.
1 - In C++ , I can write this program but in Ruby I can't:
int i,x;
int sum=0;
for( x=0 ; x <= 50 ; x++ ) {
cin >> i;
sum = sum + ( x + i ) ;
}
cout << sum;
2 - int a,b,c,d;
while( 1 ) {
cin >> a >> b >> c >>d ;
if( a < 0 || b < 0 )
break;
cout << a << " " << b << " " << c << " " << d << endl;
}
Hi
I am C++ programmer and in C++ I used some thing that I want
corresponding those things in Ruby.
please help me.
1 - In C++ , I can write this program but in Ruby I can't:
int i,x;
int sum=0;
for( x=0 ; x <= 50 ; x++ ) {
cin >> i;
sum = sum + ( x + i ) ;
}
cout << sum;
sum = 0
(0..50).each do
value = gets
value.chomp!
sum += value.to_i
end
puts sum
2 - int a,b,c,d;
while( 1 ) {
cin >> a >> b >> c >>d ;
if( a < 0 || b < 0 )
break;
cout << a << " " << b << " " << c << " " << d << endl;
}
thanks.
Based on my above example (which is Ruby for the C++ programmer) the
second should be easy for you to translate.
···
On Fri, Aug 12, 2011 at 10:10:24PM +0900, amir a. wrote:
--
Darryl L. Pierce <mcpierce@gmail.com> http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
Hi
I am C++ programmer and in C++ I used some thing that I want
corresponding those things in Ruby.
please help me.
1 - In C++ , I can write this program but in Ruby I can't:
int i,x;
int sum=0;
for( x=0 ; x <= 50 ; x++ ) {
cin >> i;
sum = sum + ( x + i ) ;
}
cout << sum;
sum = 0
50.times {|x| sum = sum + x + gets.to_i}
puts sum
2 - int a,b,c,d;
while( 1 ) {
cin >> a >> b >> c >>d ;
if( a < 0 || b < 0 )
break;
cout << a << " " << b << " " << c << " " << d << endl;
}
while true
a, b, c, d = gets.split.map {|s| s.to_i}
break if a < 0 || b < 0
STDOUT << a << " " << b << " " << c << " " << d << "\n";
end
Hi
I am C++ programmer and in C++ I used some thing that I want
corresponding those things in Ruby.
please help me.
1 - In C++ , I can write this program but in Ruby I can't:
int i,x;
int sum=0;
for( x=0 ; x <= 50 ; x++ ) {
cin >> i;
sum = sum + ( x + i ) ;
}
cout << sum;
A direct translation would be:
sum = 0
for x in 0...3
i = gets
sum = sum + (x + i.to_i)
end
print sum
But:
1) Experienced programmers never write:
sum = sum +...
They write:
sum += ....
2) In ruby for() calls each(), so ruby programmers just call each()
directly:
sum = 0
(0...3).each do |i|
x = gets
sum += (i + x.to_i)
end
A nitpick: This doesn't do exactly the same thing. The 'cin' version, if I
understand it, will read the next four things which look like integers,
separated by any whitespace, including newlines. It only seems like it
operates per-line since that's how you'd manually enter text into stdin, but
the program actually sees it as an unbroken stream. So you can enter each
number on its own line, or eight of them on the same line...
The gets version will read a single line. If there aren't enough integers,
some variables will be nil. If there are too many, some integers will be
ignored, not carried over to the next line.
Similar things apply to the first example.
Also, both Ruby and C++ have a higher-level construct than "\n" -- why not:
puts "#{a} #{b} #{c} #{d}"
···
On Friday, August 12, 2011 09:02:36 AM Dave Baldwin wrote:
On 12 Aug 2011, at 14:10, amir a. wrote:
> 2 - int a,b,c,d;
>
> while( 1 ) {
>
> cin >> a >> b >> c >>d ;
> if( a < 0 || b < 0 )
>
> break;
>
> cout << a << " " << b << " " << c << " " << d << endl;
>
> }
while true
a, b, c, d = gets.split.map {|s| s.to_i}
break if a < 0 || b < 0
STDOUT << a << " " << b << " " << c << " " << d << "\n";
end
8
[5, 6, 7, 8]
6
5 4 3 2 1
[6, 5, 4, 3]
0
1
[2, 1, 0, 1]
-e:1:in `block in <main>': undefined method `scan' for nil:NilClass
(NoMethodError)
from -e:1:in `loop'
from -e:1:in `<main>'
Kind regards
robert
···
On Fri, Aug 12, 2011 at 6:35 PM, David Masover <ninja@slaphack.com> wrote:
On Friday, August 12, 2011 09:02:36 AM Dave Baldwin wrote:
On 12 Aug 2011, at 14:10, amir a. wrote:
> 2 - int a,b,c,d;
>
> while( 1 ) {
>
> cin >> a >> b >> c >>d ;
> if( a < 0 || b < 0 )
>
> break;
>
> cout << a << " " << b << " " << c << " " << d << endl;
>
> }
while true
a, b, c, d = gets.split.map {|s| s.to_i}
break if a < 0 || b < 0
STDOUT << a << " " << b << " " << c << " " << d << "\n";
end
A nitpick: This doesn't do exactly the same thing. The 'cin' version, if I
understand it, will read the next four things which look like integers,
separated by any whitespace, including newlines. It only seems like it
operates per-line since that's how you'd manually enter text into stdin, but
the program actually sees it as an unbroken stream. So you can enter each
number on its own line, or eight of them on the same line...
The gets version will read a single line. If there aren't enough integers,
some variables will be nil. If there are too many, some integers will be
ignored, not carried over to the next line.