Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:11:18+00:00 2026-06-03T02:11:18+00:00

I implemented the Greedy algorithim in ruby using the following code: class Greedy def

  • 0

I implemented the Greedy algorithim in ruby using the following code:

class Greedy
  def initialize(unit, total, *coins)
    @total_coins1 = 0
    @total_coins2 = 0
    @unit = unit
    @total = total
    @reset_total = total
    @currency = coins.map 
    @currency.sort!
    @currency = @currency.reverse
    unless @currency.include?(1)
      @currency.push(1)
    end
  end
  def sorter
    @currency.each do |x|
      @pos = @total / x
      @pos = @pos.floor
      @total_coins1 += @pos
      @total -= x * @pos
      puts "#{@pos}: #{x} #{@unit}"
    end
    puts "#{@total_coins1} total coins"
  end
end

When I try to run the code:

x = Greedy.new("cents", 130, 50, 25, 10, 5)

I get an error:

NoMethodError: undefined method `sort!' for #<Enumerator: [50, 25, 10, 5]:map>
    from /Users/Solomon/Desktop/Ruby/greedy.rb:9:in `initialize'
    from (irb):2:in `new'
    from (irb):2
    from /Users/Solomon/.rvm/rubies/ruby-1.9.3-p125/bin/irb:16:in `<main>'

Being very new to Ruby, I have no idea what this means, nor how to fix it, because [50, 25, 10, 5].sort! is a perfectly valid method… How do I fix this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:11:19+00:00Added an answer on June 3, 2026 at 2:11 am

    Your problem is here: @currency = coins.map

    If you call map without a block, it will return an Enumerator. What is it you wanted to map here? If there’s nothing you want to do with the values of coins, just assign @currency = coins.sort.reverse and save yourself the sort! and reverse steps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented a simple sitemap class using Django's default sitemap application. As it was
I implemented push notifications using APNS-PHP http://code.google.com/p/apns-php/ on my server. I now have to
Implemented some barbuttons by using below code, UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems: [NSArray
I implemented a U.S. find near zip code search for somebody. The coordinates of
I implemented a TCP Client using a thread opening a socket to a server
Answer : Implemented using Curl... $file = http://abc.com/data//output.txt; $ch = curl_init($file); $fp = @fopen(out.txt,
I implemented an http server using javascript and Node.js. for some reason when the
I implemented TCP client to connect to the server using TcpClient ( C# .NET
Implemented UITableView with custom UITableViewCell with below code @interface ProfileSaveViewController : UITableViewController { UITableViewCell
I implemented twitter posting button on my web site using twitter codeigniter library .)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.