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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:29:20+00:00 2026-06-01T20:29:20+00:00

I was answering a question here Is there a JavaScript equivalent of rubys "#{}"

  • 0

I was answering a question here Is there a JavaScript equivalent of rubys "#{}" sequences? when i run in a problem with javascript prototype. I wanted to provide an alternative way of the first working function which gives a ruby way of printing, formatting and concatenating strings. Why is the normal function working and the prototype function not ?

<script>
function p(str){
  document.write(str.replace(/#{(\w)}/g, function(match, s){return eval(s)})+"<br>");
}

String.prototype.p = function() {
  return this.replace(/#{(\w)}/g, function(match, s){return eval(s)})+"<br>";
};


var f="foo", b="bar"
p("#{f} #{b}")
document.write("#{f} #{b}".p);

</script>

this gives

foo bar
function () { return this.replace(/#{(\w)}/g, function(match, s){return eval(s)})+"
"; }

the first line is correct, it is the concatenated string, the second line is the function itself that is printed, not the result..

  • 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-01T20:29:21+00:00Added an answer on June 1, 2026 at 8:29 pm

    "#{f} #{b}".p refers to the function itself (String.p), because “#{f} #{b}” is a string.

    You want "#{f} #{b}".p() for it to print foo bar.

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

Sidebar

Related Questions

In answering another persons question here on SO, I discovered that there is a
While answering to a question about that here: https://stackoverflow.com/a/9872630/82609 I tried to do the
This question was asked already here , but rather than answering the specific question,
I found the question here: Create GUID / UUID in JavaScript? The answer provides
As Cody Gray mentioned when answering my other question here , Raymond Chen talks
Here is a fairly easy question, though I have a hard time answering my
I'm actually answering my own question here. I must be the only person in
After answering a question about how to force-free objects in Java (the guy was
In answering this question , it got me thinking... I often use this pattern:
When answering another question, I realized that the following program does not quite do

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.