this post was submitted on 16 Sep 2023
573 points (93.9% liked)

Programmer Humor

19564 readers
1436 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] morrowind@lemmy.ml 6 points 1 year ago (2 children)
for item in array do
  puts item[:name]
end

What's with the weird syntax, isn't idiomatic ruby

array.each do |item|
  puts item[:name]
end

(or the shorthand version)?

[–] larouxn@beehaw.org 2 points 1 year ago

Was about to say as someone who's been using Ruby for over a decade, 8 of which professionally, I've never once come across a for loop. each on the other hand, all day every day.

[–] AnomalousBit@programming.dev -1 points 1 year ago* (last edited 1 year ago) (1 children)

First time I’ve ever heard someone call a for loop “weird“. They’ve been around for 50 years 😂

The whole point was on readability, not trying to make rubocop be quiet. Sure, .each is great, but I’m not sure about it being shorthand. What did you save? Like 3 characters? I find the for loop more readable unless I’m method chaining.

[–] morrowind@lemmy.ml 2 points 1 year ago* (last edited 1 year ago) (1 children)

Not in ruby, the for loop was initially put there to make it friendly for people from other languages and is discouraged. It's just syntax sugar on top of each.

By shortand version I meant

array.each(&:to_s)

(although in this case I'm not calling puts anymore)

edit: lemmy keeps putting the & there, but you know what I mean

[–] AnomalousBit@programming.dev -1 points 1 year ago

Not in Ruby

It’s valid syntax, it’s part of Ruby. It’s easy to read and familiar across many languages. Write what you want to write, I’m not sure why you feel the need to finger wag.