Hi again =)
And thanks for the great advise and how to get started!
I have now done as you said and ported it to Ruby and polished it to
the point where I can't do a better job on my own.
So can you take a look at the script again and give feedback on what
should be done differently when dealing with Ruby?
Not just Ruby, but programming generally.
Some comments in the code would go a long way toward helping someone understand what you're really expecting to happen.
Two specific examples would be the contents that you'd expect to find in config.yaml and the expected structure of the returned XML.
You should also look at open-uri, Net::HTTPS, or an HTTP client to simplify the string and command construction in get() and get_groups().
Stop using single-letter variables: a, b, c, e, h, r, v
Hash#key? is now preferred over Hash#has_key?
There's no reason to make $config be global. Coming from Perl, you may have to let go of the many sigils.
Some Ruby "idioms" for you:
users = Hash.new {|h,k| h[k] = Hash.new &h.default_proc }
-- Has the same effect (I think) that you're getting with hash_tree
-- When arguments have well-known usage AND the use is a single(few) line(s), single letter local variables are OK. (Using |hash, key| is also common.)
"#{prefix}/stuff"
-- the #{} is Ruby's string interpolation where the inner part is just ruby and if it isn't a string (i.e., doesn't result in a string when evaluated), will call to_s on the object to ask it for a string representation
-- "a/#{b}/c" is also more efficient than "a/" + b + "/c"
http://pastebin.com/xAUUQesA
If this is not a good script to OOP, can it still be done for teaching purposes?
Well, it's probably not right for OOP, but if you want to go there, I'd say expose some objects for User and Group and probably put the get and get_groups functions inside an object that exposes a repository-like API. Initialize that with $config[ARGV[0]] and expose a couple methods to iterate over all the groups (probably best to call this `each` and then look up the Enumerable module's documentation). If a Group knows how to iterate over its Users, I think that your overall script will become much simpler.
-Rob
···
On 2016-Aug-8, at 17:52 , Jasmine Lognnes <princess.jasmine.lognnes@gmail.com> wrote:
Wanted to use nokogiri but I couldn't figure out the API...
Hugs,
Jasmine =)
On 4 August 2016 at 18:10, Andy Jones <Andy.Jones@jameshall.co.uk> wrote:
+1 for porting directly to Ruby as is, with functions, at least first.
Ruby does not HAVE to be OOP. Functions are fine ... for small amounts of code, at least.
-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of Jasmine
Lognnes
Sent: 04 August 2016 3:52 pm
To: ruby-talk@ruby-lang.org
Subject: Porting a small Perl script to Ruby
Dear Ruby experts
I have written this [1] small PoC Perl script, which works, so now I
am ready to port it to Ruby.
In Perl I used functions, but that is of course not the Ruby way.
It will be my first Ruby script on my own, so can anyone help me
getting the classes/over all structure correct from the beginning?
[1]: http://pastebin.com/Qs2ReBjG
Hugs,
Jasmine =)
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>