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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:27:34+00:00 2026-06-05T05:27:34+00:00

I’m coming across the square bracket [] syntax quite a bit in Ruby, but

  • 0

I’m coming across the square bracket [] syntax quite a bit in Ruby, but it never seems to be doing the same thing. Can anyone list all the different uses for the square brackets [] in Ruby so my mind can get a handle on this seemingly endlessly versatile little symbol? (How is it possible that one symbol can do so much without the Ruby interpreter getting confused?)

Examples:

  • [] and []= methods
  • %q[...]
  • [1,2,3][0]
  • hash["a"] = 3
  • ary = []
  • /[^A-Fa-f0-9]/
  • "Is a string"[5,3]
  • 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-05T05:27:36+00:00Added an answer on June 5, 2026 at 5:27 am

    Okay, just for my own notes I have gone and had a closer look at this and, building on Holger Just’s answer, come up with the following: the use of square brackets in Ruby can be divided into 6 uses, 3 of them a part of Ruby’s method definitions and 3 of them semantic constructs.

    Method definition

    Object creation via class methods Array::[], Hash::[]

    Array.[](1,2,3) #=> [1,2,3]                        #Un-sugared notation
    Array["a","b","c"] #=> ["a","b","c"]               #Sugared equivalent
    Hash["a", 100, "b", 200] #=> {"a"=>100, "b"=>200}  
    

    Nothing to do with literal constructors, although it does the same thing.

    Element reference via instance methods Array#[], Bignum#[], Continuation#[], Fixnum#[], Hash#[], MatchData#[], Method#[], Proc#[], String#[], Struct#[], Symbol#[], Thread#[], and class methods Dir::[], ENV::[]

    ary = [1,2,"abc", [15,16,[26,27]]]  
    ary.[](2) #=> "abc"                #Un-sugared notation
    ary[2] #=> "abc"                   #Sugared equivalent
    ary[0,2] #=> [1,2]  
    ary[3][2][1] #=> 26  
    [1,2,3][0] #=> 1  
    "Is a string"[7,3] #=> "rin"  
    

    Element assignment via instance methods Array#[]=, Hash#[]=, String#[]=, Struct#[]=, Thread#[]=, and class method ENV::[]=

    ary = [1,2,3]  
    ary.[]=(1,"abc") #=> [1,"abc",3]    #un-sugared notation
    ary[2] = "def" #=> [1,"abc","def"]  #Sugared equivalent
    hash = {"a"=>1, "b"=>2}  
    hash["a"] = 3 #=> {"a"=>3, "b"=>2}  
    

    Semantic constructs

    Object creation via the array literal constructor

    ary = []  
    

    There are a bunch of literal constructors in Ruby that create an object of the relevant class via the use of (usually) a simple symbol pair, square brackets being the literal constructor for array objects: Array [], Hash {}, Proc ->(){}, Range .. and ..., Regexp //, String "" and '', Symbol : and :"".

    Object creation via the % notation

    %q[hello there you] #=> "hello there you"           # String % notation  
    %w[hello there you] #=> ["hello", "there", "you"]   # Array % notation  
    

    It is not, strictly speaking, square-bracket notation, but rather two-symbol-pair notation of which you can use square brackets if you wish. So %q@hello there you@ is equally valid.

    Ruby’s regular expressions

    /[^A-Fa-f0-9]/  
    

    Square brackets indicate character classes in Ruby regular expressions.

    I did find another use of the [], as a pattern for use in the Dir::glob method, but its supposed to act exactly as it does in regular expressions. Still, it indicates that there are possibly more uses hidden away in Ruby’s 1500+ methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I

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.