What? I just started learning Ruby, and I intend to do some sys-admin tasks also. Am I going down the wrong path?
I know Homebrew is using Ruby as base language, and doing pretty fine.
Is this misleading or what?
What? I just started learning Ruby, and I intend to do some sys-admin tasks also. Am I going down the wrong path?
I know Homebrew is using Ruby as base language, and doing pretty fine.
Is this misleading or what?
Python is stricter and more explicit. Ruby has many convenience functions that I consider messy and some even dangerous. E.g. Ruby supports the same ` `-Syntax as the Unix shell, with all it's implications.
So in Ruby you could do:
output = `/some/app #{arg}`
And in Python you would do this: import subprocess
output = subprocess.Popen(["/some/app",arg],stdout=subprocess.PIPE).stdout.read()
Yes, it's more to write but it's more powerful and you don't have code injection problems. It feels to me that Python encourages you to do the proper clean thing while Ruby tries to be very concise and quick.Also compare regular expressions.
Ruby:
if "foo" =~ /f(oo)/
puts $1
end
Oh my god, it assigns a global(?)/magic variable as a side effect. Yes, there are other ways to do it and when I write Ruby code I am using these other ways, but when I see other peoples Ruby code its done like that. I do this: match = /f(oo)/.match("foo")
if match
puts match[1]
end
Python: import re
match = re.match("f(oo)","foo")
if match:
print(match.group(1))
If you evaluate the regular expression more than once you should compile it: import re
regex = re.compile("f(oo)")
match = regex.match("foo")
if match:
print(match.group(1))
Again in Python you tend to write it the clean way. Of course it's possible to write non-clean Python code and clean Ruby code, but the culture behind the languages encourage different things. You will find a lot of Ruby libraries/frameworks that extend (monkey patch) standard types. While this is also possible in Python (for non-builtin/-binding types) it is usually not done. Look at JavaScript: Prototype monkey patched a lot of standard types and messed up a lot. jQuery learned from that and tries not to monkey patch anything.Another issue is string encodings. Note that what I write here applies to Python 3. It's a bit more complicated/less clean in Python 2.
The idea in Python is that files/network streams etc. are binary data and thus somehow encoded. But you as a programmer don't care about encodings, you want to manipulate text (or sequences of unicode code points). So you read bytes and decode them into str(ing)s. After you are done processing the text you encode with a certain encoding again and write the bytes into the file/network stream. So there is the bytes (and bytearray) class that has a decode method and there is the str class that has an encode method. str objects are always valid unicode codepoint sequences. Encoding errors can only happen on decode/encode.
In Ruby there are only Strings. These strings have an encoding attached. Binary data has the "encoding" ASCII-8BIT. You can force a wrong encoding onto a string which will cause an InvalidByteSequenceError at some later point. If you try to concatenate two String objects that have a different encoding you will get a CompatibilityError. You can only hope that all your DB drivers return proper UTF-8 strings in all cases. I was told even Perl does this better (like Python).
Furthermore in Ruby strings are mutable, in Python they are immutable. This means if you get a string passed in Ruby and want to store it in a classes attribute and want to be sure it does not change under your but you have to make a copy.
So you can clearly see what language I think is cleaner. I write Ruby code for work but when I write some shell script to automate some task/write some small GUI tool I use Python (and PyQt).
Magic locals. You weren't actually unsure if they were global, right?
Ew, I didn't realise JS had them and implemented them like that. It's supposed to remind you of Perl, which probably made more sense 20 years ago.
> It get's confusing. And even as a local it is still bad. How is it that the =~ operator can assign local variables? Can other method calls do the same? If not, why not?
Actually, they're not locals, they're "virtual variables", going by the code. You can make your own using the C API - they just call back into a function:
rb_define_virtual_variable("$&", last_match_getter, 0);
Same way things like $LOADED_FEATURES, $_ and $SAFE get managed. It's probably just as well it's not exposed Ruby-side ;)I don't think so. Ruby has for me replaced bash scripting along with sed, awk, and Perl. It's nice to do all these things with one consistent syntax. And be sure to check out Pry (http://pryrepl.org/)!
I'll give Python another look once v3 catches on.
Older systems frequently use Perl (for example, ticketing systems like RT or OTRS), or even older systems use Tcl.
Sysadmin tools are a tossup, driven mainly by whatever the rest of the ecosystem uses.