Forum: Ruby Need Assistance in converting to Ruby from Java

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ee7a3982266f34c4648374bba47dd793?d=identicon&s=25 Steven P. (helixman4838)
on 2017-02-24 18:53
Attachment: (5 KB)
Can someone help me convert this code to Ruby?
9d2d160377d47a2144b12d36ab63126f?d=identicon&s=25 Regis d. (raubarede)
on 2017-02-25 11:38
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 :)
7e17d370dc95b48cd82944a62f1caf76?d=identicon&s=25 Robert H. (shevegen)
on 2017-02-25 23:53
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 <vector>
> 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! :D

> #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

  def equals(some_time_object)

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:


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.
This topic is locked and can not be replied to.