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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:14:05+00:00 2026-06-07T05:14:05+00:00

What does $1.to_sym => args[0] and ($1.to_sym,*args,&block) do in the following line of code?

  • 0

What does $1.to_sym => args[0] and ($1.to_sym,*args,&block) do in the following line of code?

class Table
  def method_missing(id,*args,&block)
    return as($1.to_sym,*args,&block) if id.to_s =~ /^to_(.*)/
    return rows_with($1.to_sym => args[0]) if id.to_s =~ /^rows_with_(.*)/
    super
  end
  # ...
end

Context:
Ruport is a Ruby reporting library. You can use the Ruport::Data::Table class to create tabular data and convert it to different formats—text, for example:

require 'ruport'  
table = Ruport::Data::Table.new :column_names => ["country" , "wine" ], :data => [["France" , "Bordeaux" ], ["Italy" , "Chianti" ], ["France" , "Chablis" ]]  
puts table.to_text

⇒    
+--------------------+
| country |   wine   |
+--------------------+
| France | Bordeaux |
| Italy  | Chianti |
| France | Chablis |
+--------------------+

Let’s say you select only the French wines and convert them to comma-separated values:

found = table.rows_with_country("France" )
found.each do |row|
  puts row.to_csv
end

⇒
France, Bordeaux
France, Chablis

What you just did is call a method named rows_with_country( ) on Ruport::Data::Table. But how could the author of this class know you were going to have a column named country? The fact is, the author didn’t know that. If you look inside Ruport, you see that both rows_with_country( ) and to_csv( ) are Ghost Methods. The Ruport::Data::Table class is somewhat as defined above.

A call to rows_with_country( ) becomes a call to a more traditional-looking
method, rows_with(:country), which takes the column name as an argu-
ment. Also, a call to to_csv( ) becomes a call to as(:csv). If the method
name doesn’t start with either of these two prefixes, Ruport falls back
to Kernel#method_missing( ), which throws a NoMethodError. (That’s what
the super keyword is for.)

  • 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-07T05:14:08+00:00Added an answer on June 7, 2026 at 5:14 am

    Let’s look at method_missing:

    def method_missing(id,*args,&block)
      return as($1.to_sym,*args,&block) if id.to_s =~ /^to_(.*)/
      return rows_with($1.to_sym => args[0]) if id.to_s =~ /^rows_with_(.*)/
      super
    end
    

    The requisite background: method_missing is called on an object when the requested method isn’t explicitly defined. id will be a Symbol of the method called; args will be an array of the arguments, and block will be a Proc if there was a block, or nil.

      return as($1.to_sym,*args,&block) if id.to_s =~ /^to_(.*)/
    

    The execution really begins at the end, in the if: it checks the condition

    id.to_s =~ /to_(.*)/
    

    This basically does a match of the regexp on the string of the called method. x =~ y returns the integer offset in x of where y matched, if anywhere, otherwise nil. e.g.:

    > "abc" =~ /a/
     => 0
    > "abc" =~ /.$/
     => 2
    > "abc" =~ /\d/
     => nil
    

    Remember that 0 is treated as truth in boolean conditions, and so the if will only be considered to be true if the name of the called function starts with to_. The rest of the method name is captured by (.*).

    Now, we don’t explicitly save the capture groups, but Ruby borrows from Perl in that the first capture group’s contents will get saved in $1, the second in $2, etc.:

    > "abc" =~ /a(.)(.)/
     => 0
    > $1
     => "b"
    > $2
     => "c"
    

    Now, back to the line in question:

      return as($1.to_sym,*args,&block) if id.to_s =~ /^to_(.*)/
    

    So, if the called method’s name is of the form to_XYZ, it calls the as() method with the first argument set to :XYZ, and the rest of the arguments from the call appended, and the block passed through (if any).

    To continue on:

      return rows_with($1.to_sym => args[0]) if id.to_s =~ /^rows_with_(.*)/
    

    This is basically the same: if the method name is like rows_with_ABC, then it calls rows_with() with a hash {:ABC => args[0]}, where args[0] is the first argument given to the missing method call.

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

Sidebar

Related Questions

I currently have the following code: events.detect do |event| #detect does the block until
Snippet 1: module A def cm(m,ret) class_eval do define_method(m.to_sym) do return ret end end
Is there an equivalent way of achieving what HTML Tidy does to HTML code
x = hello world.to_sym puts x.class This works and allows me to concatenate the
In Ruby on Rails, where does one put the code from this snippet in
Why does A.send('!='.to_sym, B) raises a NoMethodError in Ruby 1.8.7 while A != B
I'd like to format JavaScript code of jCarousel to see what it does (to
I'm testing the following and trying to understand what it does to then apply
I am completely stumped as to why this code does not get any SDL
In the about_symbols.rb Ruby Koan (https://github.com/edgecase/ruby_koans), I have the following code: RubyConstant = What

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.