Cannot override instance variables of Struct

1 Apr 2012

You cannot override an instance variable of Struct. It's weird.

Observe:

class UpdateFriendVoteJob < Struct.new(:member_id, :question_ids, :force) def perform puts "#{question_ids}" if !question_ids.kind_of?(Array) question_ids = [ question_ids ] end puts "#{question_ids}" end end UpdateFriendVoteJob.new(1, 3, false).perform

The output is:

3 nil

I don't know why it works like that….

A workaround

class UpdateFriendVoteJob < Struct.new(:member_id, :question_ids_or_id, :force) def perform question_ids = question_ids_or_id if !question_ids.kind_of?(Array) question_ids = [ question_ids ] end puts "#{question_ids}" end end

Give it a kudos