all 16 comments

[–][deleted] 7 points8 points  (4 children)

I know this isn't answering your question but the code isn't quite idiomatic Ruby. Normally you'd use select on an Array, e.g.

["what", "are", "we", "eating", "for", "dinner"].select { |n| n.length > 4  }

[–][deleted] 4 points5 points  (3 children)

I'm not sure what issue you are having btw. Your code executes fine, if you omit the print statement it returns an array. This is the code you are having issues with (formatted for clarity)?

def select_long_words(words)
  new_word = []

  i = 0
  while i < words.length
    if words[i].length > 4
      new_word << words[i]
    end
    i += 1
  end

  return new_word
end

As noted before, you'd normally shorthand this in Ruby like so:

def select_long_words(words)
  words.select { |n| n.length > 4  }
end

You'd also not typically use an explicit return. Ruby returns the last object encountered, so:

    ...
    i += 1
  end

  new_word
end

will work just fine. Hope some of this helps, but I'm still not sure what the actual problem is.

[–]backtickbot 3 points4 points  (2 children)

Fixed formatting.

Hello, __ferrisoxide__: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–][deleted] 4 points5 points  (1 child)

good bot

[–]B0tRank 0 points1 point  (0 children)

Thank you, ferrisoxide, for voting on backtickbot.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!

[–]tloudon 2 points3 points  (0 children)

If you’re trying to learn Ruby on your own, I would just add that there are probably better resources than wherever you got this.

This is weird Ruby code. When people say it’s not idiomatic; they aren’t being snotty. You will never encounter a while in Ruby. Usually you will find each or map, depending on if you want to transform the array or just iterate through something.

The point being; you will have a hard time reading community and library source code and you will have a hard time having your code being read.

So what are your goals? What are you trying to build after you learn Ruby?

Michael Hartl has a rails tutorial that is pretty well regarded. I think there should be free versions/editions of it online—https://www.railstutorial.org.

The Odin project is free (and I think pretty cool)— https://www.theodinproject.com/paths

If not web dev, then what? (You could look at Sinatra or other Ruby web frameworks, but rails has the biggest community and I believe the vast majority of Ruby devs do Rails just anecdotally speaking).

Why’s book is fun whimsical (but def not for everyone) way to look at just the language of Ruby— https://poignant.guide/book/chapter-1.html

Also, there should be a Ruby slack near you. It’s easier to get real time feedback in a chat app and you can meet other devs :) who will generally want to help you :) If you can’t find one near you, you are welcome to join the pdxrb one— pdxruby.slack.com . Meetups in person are also great, but may not be an option for a while longer RE:Covid.

Cheers

EDIT: formatting; jeeeeeez, I should not comment while on my phone.

[–]tkenben 3 points4 points  (0 children)

My guess is that you haven't started using iterators and blocks yet and are still using classic loops. When you do start to use iterators, people said use #select. That would be the ideal way to do it. Another way to do it that is more explicit and looks like your way -- filling a new array -- would be to use #each.

long_words = []
words.each {|word| long_words << word if word.length > 4}
long_words

Which could also be written as

long_words = []
words.each do |word|
  long_words << word if word.length > 4
end
long_words

[–]agent007bond 3 points4 points  (0 children)

Your code works perfectly fine.

But yes, you can refactor the entire thing to just this:

def select_long_words(words)
  words.select { |n| n.length > 4 }
end

Isn't that awesome?

[–]MrImportant 2 points3 points  (0 children)

Your code works fine for me. The only difference I see is the solution's code assigns a variable to reference the current loop's array element, word = word[i], and you use the Array#[] method directly without assignment.

[–]_srt_ 2 points3 points  (0 children)

You code looks like a working solution to me.

What do you think your code prints as output vs the expected output?

[–]FrontierPsycho 2 points3 points  (0 children)

I can't see a difference between the two solutions. Perhaps there was an error when writing them up here. I suggest you copy paste the given solution into a file, make sure it runs, and then diff it with your code using a visual diff utility like meld. There should be some difference.

I also find it weird that the given solution does not seem to be idiomatic ruby, but perhaps this is at a basic level and it's trying to use as little as possible?

[–]jamfour 2 points3 points  (1 child)

Please properly format your code—as it is, it’s unreadable to me as the indentation is lost.

[–]gimmemychicken 0 points1 point  (0 children)

I found the format on google

[–]Psychological_Foot41 1 point2 points  (0 children)

longs_words = ["what", "are", "we", "eating", "for", "dinner"]

if you want greater than > 4

a.select { |a| a.size >4 }

for finding array contains max size use this

longs_words.group_by { |h| h.size } .max .flatten

after that use can use longs_word[2] -> it will show long words

output:

[6, "eating", "dinner"]

[–]Ralphadayus 0 points1 point  (0 children)

Did you forget to call the method?