1

I'm a newbie Rails learner. I want to try out some ruby language features, e.g. shorten the following method:

def update(params)
  attrs = []
  params[:attributes].each do |attr_hash| 
    attr = Attribute.new attr_hash
    attrs.push attr
  end
  ...
end

like:

def update(params)
   attrs = fill_attrs_from_params params
   ...
end

def fill_attrs_from_params(params)
  attrs = params[:attributes].each do |attr_hash| 
    Attribute.new(attr_hash)
  end
end

The second doesn't work as the first one however. What do I miss here?

Edit: I couldn't really decide how the second one worked, the application went buggy, and I'm a beginner with rails debugging as well.

6
  • As you're not using attrs on fill_attrs_from_params you could just iterate over params[:attributes]. Commented Jun 7, 2017 at 17:29
  • @SebastiánPalma Will it return the same array? Commented Jun 7, 2017 at 17:30
  • Nono, for that see Sergio's answer using Enumerable#map. Commented Jun 7, 2017 at 17:32
  • 2
    @SebastiánPalma: it's Enumerable#map, actually :) Commented Jun 7, 2017 at 17:33
  • 1
    Note also that "doesn't work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Commented Jun 7, 2017 at 18:25

1 Answer 1

4

That's what .map is for

  attrs = params[:attributes].map do |attr_hash| 
    Attribute.new(attr_hash)
  end

.each is very versatile. With a bit of helper code, you can make it do anything. And that is its weakness. When you think to use .each, there's almost always a method on Enumerable that does the job better with less code (of course, except the obvious use case: performing an action on each element of a collection. Here you use .each.)

Sign up to request clarification or add additional context in comments.

13 Comments

So basically using .each does not return an array, only iterating it over?
@MattSom: .each returns the original collection. Which might be what you want. But most often isn't.
@Md.FarhanMemon: With the information available here, yes. I think, something is being done to attrs in the hidden code.
@Md.FarhanMemon: question says "I want to shorten this piece of code" - the code in the answer is doing exactly the same thing, but shorter :)
It also says "I'm a newbie" maybe a small chunk of memory can be saved here...allegedly :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.