How to say # in ruby

I’m accustomed to usenet as opposed to whatever this is.

I have a variety of questions about ruby that range in subtlety. It’s
odd to think about a syntax that has, e.g., no standard. I wonder if
this place has a moderator or regulars.

My first question is a simple one: how does a person say # in ruby?
I’ve always called it a hash, which could be ambiguous. I’ve also heard
it called a pound.

TIA.

Dan F. wrote:

TIA.

Welcome to Ruby!

“This” is a mailing list called ruby-talk. It’s gated to the
comp.lang.ruby newsgroup if you’re more comfortable with USENET. There
are no moderators. There are many regulars.

Ruby does not impose a pronunciation on the # symbol. Say it however you
like. We can usually differentiate between the # symbol and the Hash
class by context.

On Feb 4, 2008, at 4:48 PM, Dan F. wrote:

I’m accustomed to usenet as opposed to whatever this is.

I have a variety of questions about ruby that range in subtlety. It’s
odd to think about a syntax that has, e.g., no standard. I wonder if
this place has a moderator or regulars.

My first question is a simple one: how does a person say # in ruby?
I’ve always called it a hash, which could be ambiguous. I’ve also
heard
it called a pound.

octothorpe

http://www.worldwidewords.org/weirdwords/ww-oct1.htm

But you can call it “hash” or “pound”. Everyone will know what you
mean.

Blessings,
TwP

Tim H. wrote:

Dan F. wrote:

TIA.

Welcome to Ruby!

“This” is a mailing list called ruby-talk. It’s gated to the
comp.lang.ruby newsgroup if you’re more comfortable with USENET. There
are no moderators. There are many regulars.

Well, you sound friendly enough. I looked with my news client at
comp.lang.ruby, and these messages are, to my surprise, there for the
perusal. I’ve updated this site to match my usenet pseudonym. I wanted
to make # the original subject, but the forum software demanded that I
embellish.

#!/usr/bin/env ruby

require ‘rubygems’
require ‘fruit_processor’

#if FILE == $0
processor = FruitProcessor.new
ARGV[0] == nil ? dir = “.” : dir = ARGV[0]
processor.pre_process dir
#end

My second question is about the shebang line: #!
I was surprised to find that it matters what’s written here when using
the windows platform. There is no env folder in the bin folder, so if
that’s a path, then it’s a path to nowhere. What “should” the first
line say?

On Tue, Feb 05, 2008, Gerry F. wrote:

#!/usr/bin/env ruby

require ‘rubygems’
require ‘fruit_processor’

#if FILE == $0
processor = FruitProcessor.new
ARGV[0] == nil ? dir = “.” : dir = ARGV[0]
processor.pre_process dir
#end

Ah, okay. You’re looking for C-style precompiler directives, yeah? No
such thing in Ruby. To do what you want, just take off the hashes.
Stylistically, indent the body of the loop as well.

Ruby’s an interpreted language with no compilation step, so everything
is interpreted top to bottom. There’s no harm in putting conditionals
like this in the main body, because if the condition fails it’ll just
skip over the body of the conditional.

My second question is about the shebang line: #!
I was surprised to find that it matters what’s written here when using
the windows platform. There is no env folder in the bin folder, so if
that’s a path, then it’s a path to nowhere. What “should” the first
line say?

Can’t help you there, unfortunately. I only keep a Windows machine for
gaming :slight_smile:

Ben

2008/2/5, Gerry F. [email protected]:

Well, you sound friendly enough. I looked with my news client at
comp.lang.ruby, and these messages are, to my surprise, there for the
perusal. I’ve updated this site to match my usenet pseudonym.

Welcome!

processor = FruitProcessor.new
ARGV[0] == nil ? dir = “.” : dir = ARGV[0]

You could as well do

dir = ARGV.shift || “.”

processor.pre_process dir
#end

My second question is about the shebang line: #!
I was surprised to find that it matters what’s written here when using
the windows platform. There is no env folder in the bin folder, so if
that’s a path, then it’s a path to nowhere. What “should” the first
line say?

I personally use Ruby on cygwin and most of the time my shebang line
looks like this:

#!/bin/env ruby

No idea about the Windows Ruby version - I haven’t used that in ages.

Kind regards

robert

Chris H. wrote:

On Feb 4, 8:20 pm, Gerry F. [email protected] wrote:

My second question is about the shebang line: #!
I was surprised to find that it matters what’s written here when using
the windows platform. There is no env folder in the bin folder, so if
that’s a path, then it’s a path to nowhere. What “should” the first
line say?

I don’t think it does matter on windows.
To execute a ruby file (say by double-clicking) you need to have the
file associations
configured.

On *nix the shebang allows a similar function.
#!/usr/bin/env is not a path, it is the ‘env’ command in /usr/bin
#!/usr/bin/env ruby will find ruby on the PATH then execute it, saves
hard-coding the ruby path (but assumes env is in a standard location).

Thanks all for responses.

I do not have an ‘env’ command in /bin/, which is to say, when I type
env into the dos prompt at ruby/bin/, I get “no such command” from dos.
Maybe I’m in the wrong bin?

This question appears to go to environmental variables. In the
documentation, it suggests to type ruby -e ‘puts $:’ to see this
information. As output, I get:
C:/ruby/lib/ruby/site_ruby/1.8
C:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt
C:/ruby/lib/ruby/site_ruby
C:/ruby/lib/ruby/1.8
C:/ruby/lib/ruby/1.8/i386-mswin32
.

, including that funny-looking period at the end.

The site_ruby directories are intended to hold modules and extensions
that you’ve added. The architecture-dependent directories (i686-linux in
this case) hold executables and other things specific to this particular
machine. All these directories are automatically included in Ruby’s
search for modules.

#end excerpt from help

I still fail to see how any of this has something to do with a shebang
line like:

#!/usr/bin/env ruby

What /usr/ would be in this context is a mystery to me.

On Feb 5, 5:22 pm, Gerry F. [email protected] wrote:

To execute a ruby file (say by double-clicking) you need to have the
I do not have an ‘env’ command in /bin/, which is to say, when I type
C:/ruby/lib/ruby/1.8/i386-mswin32
env is a standard *nix command, it is not specific to ruby

You often see perl/python/shell scripts using it too.
If your on Windows it is ignored (unless your using cygwin, or
something similar?)
I don’t usually out it in my scripts…

Gerry F. wrote:

Well, you sound friendly enough.

I’m very friendly. Just ask anybody.

My second question is about the shebang line: #!
I was surprised to find that it matters what’s written here when using
the windows platform. There is no env folder in the bin folder, so if
that’s a path, then it’s a path to nowhere. What “should” the first
line say?

I would’ve guessed that it didn’t matter, but I tried

#! garbage

and was also surprised to learn Ruby terminates the program with the
message “test.rb:1: Can’t exec garbage (fatal)”.

So I looked at the source code. In the load_file() function in ruby.c,
there is code that inspects the first line in the script. If it starts
with “#!” then the line must also contain the word “ruby”. If it
doesn’t, then Ruby calls execv() to run the “program” that it assumes
follows the #!. When execv() fails, Ruby terminates the program.

If the #! is on the 2nd line, or if the first line is

#! ruby

then everything is copacetic. That’s why

#! /usr/bin/env/ruby -w

works.

On Feb 4, 8:20 pm, Gerry F. [email protected] wrote:

My second question is about the shebang line: #!
I was surprised to find that it matters what’s written here when using
the windows platform. There is no env folder in the bin folder, so if
that’s a path, then it’s a path to nowhere. What “should” the first
line say?

I don’t think it does matter on windows.
To execute a ruby file (say by double-clicking) you need to have the
file associations
configured.

On *nix the shebang allows a similar function.
#!/usr/bin/env is not a path, it is the ‘env’ command in /usr/bin
#!/usr/bin/env ruby will find ruby on the PATH then execute it, saves
hard-coding the ruby path (but assumes env is in a standard location).

Cheers

On Feb 6, 3:45 pm, Tim H. [email protected] wrote:

Gerry F. wrote:

doesn’t, then Ruby calls execv() to run the “program” that it assumes
follows the #!. When execv() fails, Ruby terminates the program.

Thanks, that is interesting

On Sat, Feb 09, 2008, Gerry F. wrote:

Do I have it right that any line that begins with a hash that isn’t #!
is a comment, as well as the above?

Yup! And technically #! is a comment as well, it’s just a magical
comment.

Ben

Chris H. wrote:

On Feb 6, 3:45 pm, Tim H. [email protected] wrote:

Gerry F. wrote:

doesn’t, then Ruby calls execv() to run the “program” that it assumes
follows the #!. When execv() fails, Ruby terminates the program.

Thanks, that is interesting

It is! I had done the same test he did and was surprised at the result.
I’ll have a look at load_file(). So /usr/bin/env ruby is like bla, bla,
bla ruby.ginger on windows?

I wanted to get a handle on what legal comments are in ruby. I’ve also
now seen comments of the form
=begin
bla, bla, thousand points of light
democracy dominoes delilah
mitt’s gone, woo-hoo
=end
, which seem to do what // did elsewhere.

Do I have it right that any line that begins with a hash that isn’t #!
is a comment, as well as the above?

Ben B. wrote:

On Sat, Feb 09, 2008, Gerry F. wrote:

Do I have it right that any line that begins with a hash that isn’t #!
is a comment, as well as the above?

Yup! And technically #! is a comment as well, it’s just a magical
comment.

I’m not sure that I can agree with you, Ben.

static void
load_file(fname, script)
const char *fname;
int script;
{
extern VALUE rb_stdin;
VALUE f;
int line_start = 1;

if (!fname) rb_load_fail(fname);
if (strcmp(fname, "-") == 0) {

f = rb_stdin;
}
else {
FILE *fp = fopen(fname, “r”);

if (fp == NULL) {
rb_load_fail(fname);
}
fclose(fp);

f = rb_file_open(fname, “r”);
#if defined DOSISH || defined CYGWIN
{
char *ext = strrchr(fname, ‘.’);
if (ext && strcasecmp(ext, “.exe”) == 0)
rb_io_binmode(f);
}
#endif
}

if (script) {

VALUE c = 1; /* something not nil */
VALUE line;
char *p;

if (xflag) {
forbid_setid("-x");
xflag = Qfalse;
while (!NIL_P(line = rb_io_gets(f))) {
line_start++;
if (RSTRING(line)->len > 2
&& RSTRING(line)->ptr[0] == ‘#’
&& RSTRING(line)->ptr[1] == ‘!’) {
if ((p = strstr(RSTRING(line)->ptr, “ruby”)) != 0) {
goto start_read;
}
}
}
rb_raise(rb_eLoadError, “no Ruby script found in input”);
}

c = rb_io_getc(f);
if (c == INT2FIX(’#’)) {
line = rb_io_gets(f);
if (NIL_P(line)) return;
line_start++;

  if (RSTRING(line)->len > 2 && RSTRING(line)->ptr[0] == '!') {
if ((p = strstr(RSTRING(line)->ptr, "ruby")) == 0) {
    /* not ruby script, kick the program */
    char **argv;
    char *path;
    char *pend = RSTRING(line)->ptr + RSTRING(line)->len;

    p = RSTRING(line)->ptr + 1;  /* skip `#!' */
    if (pend[-1] == '\n') pend--; /* chomp line */
    if (pend[-1] == '\r') pend--;
    *pend = '\0';
    while (p < pend && ISSPACE(*p))
  p++;
    path = p;  /* interpreter path */
    while (p < pend && !ISSPACE(*p))
  p++;
    *p++ = '\0';
    if (p < pend) {
  argv = ALLOCA_N(char*, origargc+3);
  argv[1] = p;
  MEMCPY(argv+2, origargv+1, char*, origargc);
    }
    else {
  argv = origargv;
    }
    argv[0] = path;
    execv(path, argv);

    ruby_sourcefile = rb_source_filename(fname);
    ruby_sourceline = 1;
    rb_fatal("Can't exec %s", path);
}

    start_read:
p += 4;
RSTRING(line)->ptr[RSTRING(line)->len-1] = '\0';
if (RSTRING(line)->ptr[RSTRING(line)->len-2] == '\r')
    RSTRING(line)->ptr[RSTRING(line)->len-2] = '\0';
if ((p = strstr(p, " -")) != 0) {
    p++;  /* skip space before `-' */
    while (*p == '-') {
  p = moreswitches(p+1);
    }
}
  }

}
else if (!NIL_P©) {
rb_io_ungetc(f, c);
}
require_libraries(); /* Why here? unnatural */
if (NIL_P©) return;
}
rb_compile_file(fname, f, line_start);
if (script && ruby__end__seen) {
rb_define_global_const(“DATA”, f);
}
else if (f != rb_stdin) {
rb_io_close(f);
}

if (ruby_parser_stack_on_heap()) {
    rb_gc();
}

}

end function load_file in ruby.c

What good’s a comment if you have to say the right thing?

rb_fatal(“Can’t exec %s”, path);

Gerry F.

Robert K. wrote:

I’m not sure what you’re at here. You posted code from the Ruby
interpreter but the magical bit of shebang is done by the shell or OS
not Ruby.

Bear in mind that if the shell started the program and the program
specified on the shebang line wasn’t the ruby interpreter, the code
quoted above doesn’t run.

Since it is running, somehow the ruby interpreter has been started and
given a file containing text that may or may not actually be Ruby code.
This could happen if you started ruby by explicitly executing the “ruby”
command, or if you’re running on Windows where there is no shell to do
shebang interpreting for you.

That bit of code inspects the shebang line and, if the word “ruby”
doesn’t appear on the line, it tries to execute what it assumes is a
program.

If you have access to Ruby on Windows, try it yourself.

2008/2/11, Gerry F. [email protected]:

Ben B. wrote:

On Sat, Feb 09, 2008, Gerry F. wrote:

Do I have it right that any line that begins with a hash that isn’t #!
is a comment, as well as the above?

Yup! And technically #! is a comment as well, it’s just a magical
comment.

I’m not sure that I can agree with you, Ben.

What good’s a comment if you have to say the right thing?

I’m not sure what you’re at here. You posted code from the Ruby
interpreter but the magical bit of shebang is done by the shell or OS
not Ruby. See for example:

http://www.google.com/codesearch?hl=de&q=+lang:c+package:kernel+"%23!"+show:BxIe8bIKbEg:X-2Nm3hrV4s:RaABaUcqtps&sa=N&cd=2&ct=rc&cs_p=http://kernel.org/pub/linux/kernel/v2.4/linux-2.4.34.1.tar.bz2&cs_f=linux-2.4.34.1/fs/binfmt_script.c

Kind regards

robert

Robert K. wrote:

2008/2/11, Gerry F. [email protected]:

Ben B. wrote:

What good’s a comment if you have to say the right thing?

I’m not sure what you’re at here. You posted code from the Ruby
interpreter but the magical bit of shebang is done by the shell or OS
not Ruby. See for example:

http://www.google.com/codesearch?hl=de&q=+lang:c+package:kernel+"%23!"+show:BxIe8bIKbEg:X-2Nm3hrV4s:RaABaUcqtps&sa=N&cd=2&ct=rc&cs_p=http://kernel.org/pub/linux/kernel/v2.4/linux-2.4.34.1.tar.bz2&cs_f=linux-2.4.34.1/fs/binfmt_script.c

Thanks for your continued attention, robert. That link was very
interesting. (Ich hatte google noch nie auf deutsch gesehen.) One
snippet I found there was this one:

288: {
printk( " no PCI bus?@#!\n");
return NumberOfAdapters;

This has #! looking very uncommentlike. Is #! a token separate from # ?
(Does ruby have tokens at all?)

Should I maybe start a new thread with this, as the discussion has come
a long way from how a person says # in ruby?

cordially,

On 11.02.2008 14:25, Tim H. wrote:

Since it is running, somehow the ruby interpreter has been started and
given a file containing text that may or may not actually be Ruby code.
This could happen if you started ruby by explicitly executing the “ruby”
command, or if you’re running on Windows where there is no shell to do
shebang interpreting for you.

That bit of code inspects the shebang line and, if the word “ruby”
doesn’t appear on the line, it tries to execute what it assumes is a
program.

Sure, but from the thread it’s not clear whether Ben did mean that bit
of magic or whether Gerry was aware of the other magic of shebang.
That’s why I asked.

Cheers

robert

Tim H. wrote:

Robert K. wrote:

I’m not sure what you’re at here. You posted code from the Ruby
interpreter but the magical bit of shebang is done by the shell or OS
not Ruby.

Bear in mind that if the shell started the program and the program
specified on the shebang line wasn’t the ruby interpreter, the code
quoted above doesn’t run.

Since it is running, somehow the ruby interpreter has been started and
given a file containing text that may or may not actually be Ruby code.
This could happen if you started ruby by explicitly executing the “ruby”
command, or if you’re running on Windows where there is no shell to do
shebang interpreting for you.

That bit of code inspects the shebang line and, if the word “ruby”
doesn’t appear on the line, it tries to execute what it assumes is a
program.

If you have access to Ruby on Windows, try it yourself.

Thanks for your continued interest.

I created 2 scripts:
#! bla bla bla ruby

puts “Hello World”
END

ruby test1.rb

ruby test1.rb >text55.txt 2>text56.txt

This runs without objection.

#! bla bla bla ginger

puts “Hello World”
END

ruby test2.rb

ruby test2.rb >text55.txt 2>text56.txt

I thought this would fail, but it didn’t. I changed it to

#! windows

puts “Hello World”
END

ruby test2.rb

ruby test2.rb >text55.txt 2>text56.txt

, and got:
test2.rb:1: Can’t exec windows (fatal)

My point is that a comment should never be able to halt execution.

cordially,

Thanks for your continued attention, robert. That link was very
interesting. (Ich hatte google noch nie auf deutsch gesehen.) One
snippet I found there was this one:

288: {
printk( " no PCI bus?@#!\n");
return NumberOfAdapters;

This has #! looking very uncommentlike. Is #! a token separate from # ?
(Does ruby have tokens at all?)

That’s kernel driver code, not ruby, right? In that context, I’m
pretty sure that @#! is cartoonish swearing, not actually a comment or
anything meaningful.