News for hackers. Stuff that matters.
Re:Ruby: A great language for shell scripts by lr4444lr on Saturday June 22, 2024 @10:02PM (#40764079)
Not denying Ruby is good, but other than process forking, why should I prefer it to Python?
Re:Ruby: A great language for shell scripts by katbyte on Saturday June 22, 2024 @10:26PM (#40764211)
Not using white space as a delimiter
Re:Ruby: A great language for shell scripts by ianschmitz on Sunday June 23, 2024 @02:31AM (#40765194)
Ruby is surprisingly picky with white space
Re:Ruby: A great language for shell scripts by manume on Sunday June 23, 2024 @03:02AM (#40765321)
Can you give an example? I can't think of a single situation where whitespace matters in Ruby (unless of course you forget to put a space between two commands or something silly).
Re:Ruby: A great language for shell scripts by codesnik on Sunday June 23, 2024 @06:38AM (#40766377)
if foo is a method then

`foo + bar` and `foo+bar` are `foo()+bar`, but `foo +bar` is `foo(+bar)`

ternary ? : also has some interesting whitespace dependent mixups with symbols, but I cannot remember what. I think that parser has many gotchas like that, but they are really really rare to bite you, because ruby's magic follows human intuition as much as possible.

Re:Ruby: A great language for shell scripts by p_l on Sunday June 23, 2024 @09:38AM (#40767394)
still less annoying than Python's semantic whitespace
Re:Ruby: A great language for shell scripts by ezrast on Sunday June 23, 2024 @12:29PM (#40768608)
It's not really a problem in practice (and I love Ruby), but it's still wild to me that they made the parser do this:

    $ irb
    irb(main):001:0> def foo(x=70) = x
    => :foo
    irb(main):002:0> i = 2
    => 2
    irb(main):003:0> foo / 5/i
    => 7
    irb(main):004:0> foo /5/i
    => /5/i
Re:Ruby: A great language for shell scripts by katbyte on Sunday June 23, 2024 @01:06PM (#40768877)
Yea but it’s still not a delimiter
Re:Ruby: A great language for shell scripts by corytheboyd on Saturday June 22, 2024 @11:03PM (#40764419)
It’s just preference at that point IMO, and I’d base it off “what language are the rest of the scripts using?”.
Re:Ruby: A great language for shell scripts by manume on Sunday June 23, 2024 @03:04AM (#40765324)
For 99% of use cases (especially when writing shell scripts) it doesn't matter, just pick the one you know better. Both are nicer than Bash though. :)
Re:Ruby: A great language for shell scripts by cyclotron3k on Sunday June 23, 2024 @04:29AM (#40765710)
Personally, I find Ruby's syntax more natural, (I'm going to be heavily biased though, having written Ruby for 10+ years). But for example, let's say I wanted to make a hash (dict) of files, keyed by their size (for some unknown reason), in Ruby it would look like:

  Pathname.glob('*').filter { |f| f.file? }.each_with_object({}) { |f, h| h[f.size] = f }
Whereas the equivalent Python would be:

  result = {}
  for file_path in glob.glob('*'):
    if os.path.isfile(file_path):
      result[os.path.getsize(file_path)] = file_path
Or capitalizing a string in Ruby:

  string.split(' ').map(&:capitalize).join(' ')
And in Python:

  words = string.split(' ')
  capitalized_words = [word.capitalize() for word in words]
  result = ' '.join(capitalized_words)
Python seems to be more convoluted and verbose to me, and requires more explicit variable declarations too. With the Ruby you can literally read left to right and know what it does, but with the Python, I find I have to jump about a bit to grok what's going on. But maybe that's just my lack of Python experience showing.
Re:Ruby: A great language for shell scripts by ndand on Sunday June 23, 2024 @08:34AM (#40766993)
You can capitalize a string in Python using functional style

  ' '.join(map(str.capitalize, string.split(' ')))
which is similar to the example in Ruby, except the operations are written in reverse order.
Re:Ruby: A great language for shell scripts by pseudalopex on Sunday June 23, 2024 @08:57AM (#40767146)

    result = {os.path.getsize(f): f for f in os.listdir() if os.path.isfile(f)}

    result = ' '.join(word.capitalize() for word in string.split(' '))
    result = ' '.join(map(str.capitalize, string.split(' ')))
    result = string.title()
Re:Ruby: A great language for shell scripts by sgarland on Sunday June 23, 2024 @09:15AM (#40767254)

  from string import capwords 
  result = capwords(string)
This does the split/capitalize/join dance, all in one.

The file example you gave could also be turned into a dict comprehension if desired. I’m on mobile, but I think this would work.

  result = { f:os.path.getsize(f) for f in glob.glob(“*”) if os.path.isfile(f) }