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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:42:27+00:00 2026-06-01T06:42:27+00:00

I am going through Programming Ruby – a pragmatic programmers guide and have stumbled

  • 0

I am going through Programming Ruby – a pragmatic programmers guide and have stumbled on this piece of code:

class SongList
  def [](key)
    if key.kind_of?(Integer)
      return @songs[key]
    else
      for i in 0...@songs.length
        return @songs[i] if key == @songs[i].name
      end
    end
    return nil
  end
end

I do not understand how defining [ ] method works?

Why is the key outside the [ ], but when the method is called, it is inside [ ]?

Can key be without parenthesis?

I realize there are far better ways to write this, and know how to write my own method that works, but this [ ] method just baffles me… Any help is greatly appreciated, thanks

  • 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-01T06:42:28+00:00Added an answer on June 1, 2026 at 6:42 am

    Methods in ruby, unlike many languages can contain some special characters. One of which is the array lookup syntax.

    If you were to implement your own hash class where when retrieving an item in your hash, you wanted to reverse it, you could do the following:

    class SillyHash < Hash
    
      def [](key)
        super.reverse
      end
    
    end
    

    You can prove this by calling a hash with the following:

    a = {:foo => "bar"}
     => {:foo=>"bar"} 
    a.[](:foo)
     => "bar" 
    a.send(:[], :foo)
     => "bar" 
    

    So the def [] defined the method that is used when you do my_array["key"] Other methods that may look strange to you are:

    class SillyHash < Hash
    
      def [](key)
        super.reverse
      end
    
      def []=(key, value)
        #do something
      end
    
      def some_value=(value)
        #do something
      end
    
      def is_valid?(value)
        #some boolean expression
      end
    
    end
    

    Just to clarify, the definition of a [] method is unrelated to arrays or hashes. Take the following (contrived) example:

    class B
      def []
        "foo"
      end
    end
    
     B.new[]
     => "foo" 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going through Programming from ground up. Now I have the following code,
I'm going through Pragmatic Programming Erlang where there is a function defined like this:
I'm new to programming & i found this code when i was going through
I was going through an example from the Programming in Ruby book: def fib_up_to(max)
Hi I am going back through C programming exercises and in this particular program
I noticed this while using Qt, going through the code examples. When they try
Preamble So, I'm going through The C Programming Language and this quote struck me:
I was going through the Programming Ruby book and I'm having problems understanding the
I was going through the Programming Interviews Exposed book. There's a code given for
While going through many resources on multithreaded programming, reference to volatile specifier usually comes

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.