Project

General

Profile

Actions

Feature #15983

closed

Can we have a similar syntax (string interpolation) the way V language has?

Added by mechanicles (Santosh Wadghule) almost 5 years ago. Updated over 4 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:93527]

Description

Sorry, guys, This is the first feature that I am writing in this Ruby issue tracking system, and If I make any mistake, please understand :).

Today I was going through this language, i.e. V language (mainly their website) and found its basic code like below,

fn main() {
	areas := ['game', 'web', 'tools', 'science', 'systems',
	          'embedded', 'GUI', 'mobile'] 
	for area in areas {
		println('Hello, $area developers!')
	}
}

I like this kind of string interpolation which looks so simple to write. If we try to write the same code in Ruby, we need to write like this,

areas = ['game', 'web', 'science', 'system']

areas.each do |area|
  puts "Hello, #{area} developers!"
end

Instead of using #{area} syntax for string interpolation, can we have something like $area? This might save some keystrokes.

I know that Ruby has already occupied this $ character. But we can use another character which is not used in Ruby.

We can close this feature request if we already discussed on such topic before.

Updated by duerst (Martin Dürst) almost 5 years ago

There are other languages that use $ in string interpolation. One of them is Perl. Perl is much more famous and widely used than V. Ruby was created as a better Perl. $variable_name may work (most of the time, but not always) in languages with spaces between words, but it doesn't work well in a language that's written without spaces, such as Japanese. My guess is that this is one of the reasons for why Matz choose an explicit terminator ('}') for interpolations.

Updated by shevegen (Robert A. Heiler) almost 5 years ago

Let's look at this purely from a syntax point of view first, so that we can avoid
other issues such as backwards incompatible changes (which may be a big reason
why this request will not be implemented; but you have to ask matz).

I think you have made a good point that e. g.

$foo = 'world'

puts "hello $foo"

would be simpler to type than e. g.

puts "hello #{$foo}"

or

foo = 'world'

puts "hello #{foo}"

Actually, for global variables this is already possible and has been
for many years; for @instance_variables this also seems to work... I
thought it would not work but I just tested this:

@foo = 'world'

puts "hello #@foo"

And that works.

Martin pointed out that Perl allows for string interpolation like that,
although it still seems different to this behaviour:

areas := ['game', 'web', 'tools'] 

for area in areas {

    println('Hello, $area developers!')

Since we do not need special access modifiers for the variable "areas",
whereas I seem to remember in perl we had to use some identifier
such as $areas or something like that; or @areas or something (it
has been literally like almost +15 years since I last wrote perl
code ...).

Having said that, I think that it is probably not easily doable to
interpolate without #{} for non-global variables, as otherwise
it might have been done yet.

Adding another syntax identifier as proposed, would, in my opinion,
not be a good idea, since the scope of the change is motivated
primarily by the possibility to avoid #{} only. #{} can be annoying,
but having to add a new character solely for this would be bad as
well, I think. Then there is also the issue of backwards compatibility,
so I think while it is an interesting idea, it is most likely not
a very realistic one. But that is just my opinion - ultimately you
have to ask matz. :)

Updated by ko1 (Koichi Sasada) over 4 years ago

  • Status changed from Open to Rejected

I know that Ruby has already occupied this $ character. But we can use another character which is not used in Ruby.

Please make new ticket if you have good character instead of $.

Thanks.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0