Is Array constants in the model can be changed during runtime?

42 views
Skip to first unread message

Роман Ярыгин

unread,
Feb 3, 2014, 4:36:25 AM2/3/14
to rubyonra...@googlegroups.com

Hello everyone!

 

I have strange problem. Here is the code: 

 

model: 

 

class ItServiceJob < ActiveRecord::Base
TYPES = [["Important", 0], ["Not important", 1], ["SERGEY", 2]]

belongs_to :user
end

view (haml): 

 

  %div{:role => "main", :class => "ui-content"}
    - job_types = ItServiceJob::TYPES
    - job_types.pop if (!can?(:manage, :it_service) || !can?(:born, :sergey))
    = job_types
    = form_for ItServiceJob.new, url: "#" do |f|
      
      %p
        = f.label :type
        = f.select :type, job_types

Now, when I refresh the page every time, the array pops one by one item and finally get empty! But, why? Why the constant changes, not variable? What I'm doing wrong?

Marco Antonio Almeida

unread,
Feb 3, 2014, 4:49:36 AM2/3/14
to rubyonra...@googlegroups.com
Hi,

Constants in Ruby are "contantish". And in your case you're passing the array reference to job types, so any changes that you do in job_types it goes to ItServiceJob::TYPES.

With that being said what you need to do is duplicate the ItServiceJob::TYPES. Change the job_type assignment with:
job_types = ItServiceJob::TYPES.dup
Regards,
Marco Antonio Almeida
+45 31 65 28 84
Twitter: @marcoafilho | LinkedIn: marco-antonio-almeida-filho


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fe1e76b2-f6e5-430d-8171-1f6616ce0de7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Frederick Cheung

unread,
Feb 3, 2014, 4:53:21 AM2/3/14
to rubyonra...@googlegroups.com


On Monday, February 3, 2014 9:36:25 AM UTC, Роман Ярыгин wrote:

Now, when I refresh the page every time, the array pops one by one item and finally get empty! But, why? Why the constant changes, not variable? What I'm doing wrong?


What is constant is that ItServiceJob::TYPES always gives you the same object (in terms of it object_id), but there is nothing to stop you modifying the object.

Furthermore,  job_types = ITServiceJob::TYPES isn't copying ItServiceJob::TYPES, it is just creating a local variable that references the exact same ruby object. You can copy the array with .dup (this is a shallow copy). If you want to prevent modifications to an object you need to freeze it (by calling .freeze on it). After this any attempts to modify the object will raise an exception

Fred 

Arun kant sharma

unread,
Feb 3, 2014, 5:50:04 AM2/3/14
to rubyonra...@googlegroups.com
Sorry for off topic reply, but how do you quote code using gmail? or I should use web interface for posting in group.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Colin Law

unread,
Feb 3, 2014, 6:06:51 AM2/3/14
to rubyonra...@googlegroups.com
On 3 February 2014 10:50, Arun kant sharma <iaru...@gmail.com> wrote:
> Sorry for off topic reply, but how do you quote code using gmail? or I
> should use web interface for posting in group.

Click on the three dots on the bottom left of the input text window to
expand the text.

Colin
> https://groups.google.com/d/msgid/rubyonrails-talk/CAGHQMcibCKKG_nC%3Dwhc%2BY8WT%3DFbz6cxydm%3D3721Fc5oodc0U3A%40mail.gmail.com.

Arun kant sharma

unread,
Feb 3, 2014, 6:15:31 AM2/3/14
to rubyonra...@googlegroups.com
Oops, I meant code snippets. How to insert them in gmail?


Colin Law

unread,
Feb 3, 2014, 6:17:28 AM2/3/14
to rubyonra...@googlegroups.com
On 3 February 2014 11:15, Arun kant sharma <iaru...@gmail.com> wrote:
> Oops, I meant code snippets. How to insert them in gmail?

Copy/Paste. Best to keep it plain text. IMO at least.
> https://groups.google.com/d/msgid/rubyonrails-talk/CAGHQMcivVOu0s%3DDO44WvXF3Q8yFAjaRE-iw2iWpCAcVobDxp6g%40mail.gmail.com.

Роман Ярыгин

unread,
Feb 3, 2014, 7:17:53 AM2/3/14
to rubyonra...@googlegroups.com
Thank you for the answers! Will try at work tomorrow!

P.S. I just copy-paste formatted text from another forum =)
Reply all
Reply to author
Forward
0 new messages