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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:29:42+00:00 2026-06-12T13:29:42+00:00

I created the following extension class String def is_a_number? s # check if string

  • 0

I created the following extension

class String
  def is_a_number? s  # check if string is either an INT or a FLOAT (12, 12.2, 12.23 would return true)
    s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
  end
end

How can I make it work as a chained method?

is_a_number?("10") # returns true
"10".is_a_number? # returns an error (missing arguments)

Update

Thanks sawa, mikej and Ramon for their answers. As suggested, I changed the class to Object and got rid of the argument (s):

class Object
  def is_a_number?  # check if string is either an INT or a FLOAT (12, 12.2, 12.23 would return true)
    to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) != nil
  end
end

It now works perfectly fine:

23.23.is_a_number? # > true

Thanks guys…

  • 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-12T13:29:43+00:00Added an answer on June 12, 2026 at 1:29 pm

    When you write "10".is_a_number?, you already have the object "10" you want to check for, which is the receiver of is_a_number?, so your method doesn’t need to take any parameters.

    Because match is an instance method on String, you don’t need to specify a receiver for it. It will just operate on the same object on which is_a_number? was called. Because you know you already have a String object, the to_s isn’t needed either.

    Just write it as:

    class String
      # check if string is either an INT or a FLOAT (12, 12.2, 12.23 would return true)
      def is_a_number? 
        match(/\A[+-]?\d+?(\.\d+)?\Z/) != nil
      end
    end
    

    Ramon’s suggestion that you may want to put your extension on Object rather than on String is a good point if you don’t know if the object you’re testing is going to be a string.


    Also, what you’re describing isn’t really what is meant by method chaining; it’s just calling a method on an object. Method chaining is where the return types of methods are set up so that several methods can be called in sequence e.g in Rails, something like

    User.where(:name => 'Mike').limit(3) # find the first 3 Mikes
    

    is an example of method chaining.

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

Sidebar

Related Questions

I create the following class: class Image(object): def __init__(self, extension, data, urls=None, user_data=None): self._extension
I created a following class within a namespace Global namespace Global { public static
I created the following method for retrieving stored settings from the database: public String
I created the following script to query a table and return the first 30
I have the following HTMLHelper extension to create a submit button that would work
I created the following extension method for a ViewPage: using System.Web.Mvc; namespace G3Site {
I have the following code: public class OMyObject { public int Id { get;
I have the following classes: public class Note { public string Text { get;
I've made the following extension method ... public static class ObjectExtensions { public static
How would you create an extension method which enables me to do the following

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.