Can someone help me convert this code to Ruby?
Steven Patel wrote in post #1185593:
Can someone help me convert this code to Ruby?
t ave misunderstand your teacher talk !!!
It is C++ code, not java
It is way too long to convert this C++ code 1:1 to ruby. Sounds like a
homework exercise.
But I give a few pointers anyway.
#include
using namespace std;
^^^ Ruby load and require statements!
You can also “re-assign” namespaces, like:
module Foo; end
X = Foo
Now X is like Foo!
#include “Time.h”
See require ‘time’, date, datetime.
Somewhere are the equivalent time thingies.
class Date
This is easy!
It is:
class Date; end
in Ruby.
Ok some annoying constructors in C++ … glad that
ruby does not require of you to pre-define the methods.
void print() const;
bool equals(Date other) const;
def print
end
def equals(some_time_object)
end
You have to do the “const” checking and bool
checking within the method.
void Date::print() const
{
cout << year << “/” << month << “/” << day;
}
That almost works in ruby too!
If year is in string representation.
Otherwise it could be:
year = ‘2016’
month = ‘08’
day = ‘05’
puts year << “/” << month << “/” << day
Ok skipping ahead …
cin >> sh >> sm >> eh >> em;
getline(cin, description);
User input in Ruby goes:
$stdin.gets.chomp
For example. Or just gets.chomp. Or Readline module, which is
more convenient.
Anyway, I assume that you only randomly loaded some example
here but once you understand ruby, I would not recommend
a 1:1 clone of the C++ code. Simply write the functionality
that this C++ code does into new ruby code.
It does not seem to be very complex. Just a bit user input
and the rest is determinate.