1

  I want to make an array where to keep scores for every user. An example of the array would be ScoreArray["[email protected]"] = 10. To do this I need to instantiate the Array first. So I tried something like:

ScoreArray = Array.new

    @sugested.each do |gg|
      nr = 0
      @followees.each do |ff|
        if (ff.email == gg.email) then nr = nr + 1
        end
      end
      if(gg.following.count != 0) then
        score = ( nr/@followees.count ) * ( gg.followers.count / gg.following.count)
        ScoreArray[gg.email] = score
        pry.bind
      else score = 0
      end 
    end

  All this code is inside a method called candidates . When I try to run rails server I get the following error message on the page where I invoke this method :

home/flo/Ruby/Licenta/honk_app/app/controllers/application_controller.rb:45: dynamic constant assignment ScoreArray = Array.new ^

  Any ideas how can I avoid this problem? And why is it doing this?(from what I've read is because it's inside a method and ruby doesn't like instantiating a "constant" each time a method is called. The thing is , this is not a constant ... for each user that logs in I will have a separate array).

6
  • What is the scope of this array (i think you actually want a hash) supposed to be? Commented Feb 22, 2015 at 10:40
  • I did want to make a hash first. But I have to order it by score(the values in it) after, so I decided an Array would be better . The order is necessary because I will have to show Users in order based on that score Commented Feb 22, 2015 at 10:41
  • U can try something like this Hash[actual_hash.sort_by{|k, v| v}.reverse], this will give you a hash sorted by values, in your case score is a value Commented Feb 22, 2015 at 10:46
  • @Sontya it returns a hash ? And if I do .each on that Hash I get the elements in order(by score) ? Commented Feb 22, 2015 at 10:52
  • 1
    If you don't want a constant, why make it a constant? (if it starts with an uppercase letter it's a constant) Commented Feb 22, 2015 at 10:55

2 Answers 2

2

In ruby a leading capital letter denotes a constant - if you don't want a constant then start with a lowercase letter (if a local variable isn't sufficient for your purpose, consider an instance variable)

In addition arrays can't be used as you show them

some_array[gg.email]

Will raise an exception if gg.email is a string

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

1 Comment

true indeed :P I changed it back to a hash and did as Sontya told me :P .
1

Try this

score_hash = Hash.new
score_hash[gg.email] = score
sorted_hash = Hash[score_hash.sort_by{|k, v| v}.reverse]

1 Comment

dynamic constant assignment ScoreHash = Hash.new it still has the same dynamic constant assignment error

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.