Forum: Ruby Need Assistance in converting to Ruby from Java

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
82a001fadd5b301d1f6a83ca3f1e5e6f?d=identicon&s=25 Steven Patel (helixman4838)
on 2017-02-24 18:53
Attachment: Appointments.java (5 KB)
Can someone help me convert this code to Ruby?
B078cb4f4fb473c7a54d1fc36d10c70e?d=identicon&s=25 Regis d'Aubarede (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 :)
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (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.

:D

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