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 7550551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:14:29+00:00 2026-05-30T10:14:29+00:00

class Klass attr_accessor :keys def change_keys(opt) if opt == 1 keys = [keys[0], keys[keys.length

  • 0
class Klass
  attr_accessor :keys
  def change_keys(opt)
    if opt == 1
      keys = [keys[0], keys[keys.length - 1]]
    else
      tmp = keys[0]
      keys[0] = keys[keys.length-1]
      keys[keys.length-1] = tmp
    end
    keys
  end
end
klass = Klass.new
klass.keys = [1,2,3,4,5]

# puts klass.change_keys(1)
# puts klass.change_keys(2)

This is not working at all, the error says: undefined method ‘[]’ method for nil:NilClass

  • 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-05-30T10:14:30+00:00Added an answer on May 30, 2026 at 10:14 am

    Ruby interprets keys[0] and the others in line 5 as local variables because it has seen a keys = ... . There is an ambiguity in Ruby grammar when it comes to differentiate local variables from method calls without arguments that gets disambiguated by that heuristic. that is, if the parser sees an assignment to that identifier then is a local variable if not is a method call.

    You can solve this by referring to self.keys instead. to make clear that you want to use the accessor method.

    class Klass
      attr_accessor :keys
      def change_keys(opt)
        if opt == 1
          self.keys = [keys[0], keys[keys.length - 1]]
        else
          tmp = keys[0]
          keys[0] = keys[keys.length-1]
          keys[keys.length-1] = tmp
        end
        keys
      end
    end
    klass = Klass.new
    klass.keys = [1,2,3,4,5]
    
    puts klass.change_keys(1)
    puts klass.change_keys(2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this class: class ComponentLibrary def self.register(klass); ...; end end And
This is what I have so far: def get_concrete_name_of_class(klass): Given a class return the
Consider the following code: def create_class(class_name, superclass, &block) klass = Class.new superclass, &block Object.const_set
I have the following Code: def link_to_add_fields(name, f, association) new_object = f.object.class.reflect_on_association(association).klass.new fields =
i load a class using Class.forName(klassname,false,loader) After this i create an instance using klass.newInstance();
I have the following module: # lib/translator/sms_command.rb module Translator module SmsCommand def self.included(klass) puts
Just playin around class def func(*args, **kwargs): print args, kwargs class Klass(func): pass it
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
Let's say I have a class named Klass , and a class called Klass2
class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end

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.