[ruby-talk:00383] FromJapan: #1
From:
gotoken@... (GOTO Kentaro)
Date:
1999-06-04 07:48:50 UTC
List:
ruby-talk #383
Hi folks!
Today, I pick up 11 topics -- 1 contrib and 10 Q&As.
______________________________________________________________________
Contributions
-------------
[ruby-list:14713] ruby/gtk 0.18
Kand-san put ruby-gtk-0.18.tar.gz into contrib.
______________________________________________________________________
Q and A
-------
[ruby-list:14481] count lines
Question:
Would you tell me simple way to count number of lines
in a file?
And why does "foo\n\n\n".split(/\n/).size returns zero?
Answer:
Probably the fastest way may be:
open("afile").read.delete("\n").size
Note that the above code does not count the last line
if `afile' is not ended by "\n".
And "\n\n\n".split(/\n/, -1).size returns four. See the
reference manual.
______________________________________________________________________
[ruby-list:14477] tiny ruby
Question:
I'm planning to archive ruby for PocketBSD, which is
customized FreeBSD for NEC's handheld machine Mobil Gear.
Is this plan is permitted?
What is recommended in term of the license?
Answer:
Yes, permitted. Give a distinct name to the package if you
divert source code of ruby to different product.
It is written README. Please read REAME again.
______________________________________________________________________
[ruby-list:14513] Hater
Question:
A pal hates ruby. I'm so blue that I don't wanna go to
school. Could you give me any advice?
Reaction:
A hundred answers are replied! I pick up impressive massages.
* You are enviable. In my school days, none talk about
what a programming language should be.
* The value of ruby doesn't depend on his liking.
* Don't be so serious. I dislike Perl, but I shall never
hate a man by reason of he's loving Perl.
* No language satisfies all requests.
______________________________________________________________________
[ruby-list:14521] Ruby style?
Question:
I'd like to write ruby ruby code. Would you correct my
program?
#!/usr/local/bin/ruby
# analyze command line argument
column=[]
column = ARGV.dup
in_file = column[0] # file name
num = column[1] # number of space
# create output filename as input filename + .out
out_file = in_file + ".out"
print in_file,"--->",out_file,"\n"
# open both infile and outfile
fin = open(in_file,"r")
fout = open(out_file,"w")
# string to replace
space=" " * num.to_i
# for each line of infile, replace and write
while fin.gets
sub(/\r\n/,"\n")
# replace TAB to space
gsub(/\t/,space)
printf(fout,"%s",$_)
end
# close files
fout.close
fin.close
Answer:
I would write as follows:
--- tab2space.rb ---
#!/usr/bin/env ruby
require 'getopts'
getopts(nil, "n:")
tabwidth = $OPT_n ? $OPT_n.to_i : 8
while gets
while sub!(/\t/) { " " * (tabwidth - $~.begin(0) % tabwidth) }
end
print
end
---
$ ruby -i.bak tab2space -n 4 foo.c
Uniformly replace with fixed number of space makes indent
uneven, so number of space is roughly caliculated.
The above is for Ruby 1.3.x. Use the below for 1.2.x.
while pos = $_.index(?\t)
$_[pos] = " " * (tabwidth - pos % tabwidth)
end
______________________________________________________________________
[ruby-list:14525] Re: ruby style?
Question:
What does `#!/usr/bin/env ruby' cause?
Answer:
`ruby' will be invoked as so long as PATH includes the
directory on which ruby exits, with the proviso `env'
must be on /usr/bin/.
______________________________________________________________________
[ruby-list:14525] Re: ruby style?
Comment:
#!/usr/bin/ruby -i.back
works but
#!/usr/bin/env ruby -i.back
Comment:
#!/usr/bin/env ruby
$-i = ".back"
Comment:
I heard `#!' line allows `one command' and `at most one
option' in a shell script.
Comment:
To avoid that limitation, The following is useful.
#!/bin/sh
# shell script around here
exec ruby -i.bbb -x $0 $*
Here anything skiped!
You can write anything freely!
#! ruby
# ruby script starts heare
p $-i
p $0
p $*
______________________________________________________________________
[ruby-list:14586] Ruby, hardware description language
Comment:
I found a language Ruby, which is for circuit description.
http://www.comlab.ox.ac.uk/oucl/users/geraint.jones/ruby/
______________________________________________________________________
[ruby-list:14597] TkAfter munched memory.
Question:
I heard TkAfter is better than Tk.after because the latter
costs for each registrating of proc. But the following
munches memories.
require "tk"
TkAfter .new(10, -1, proc{p 'xxx'}) .start
Tk.mainloop
Answer:
It is bug of tcltklib. I fixed it. Thank you for the report.
By the way, TkAfter is hardly used since thread provides same
functions.
______________________________________________________________________
[ruby-dev:6953] RString#len
Comment:
struct RString's member len is declared as int. It should
be a integer type whose size as same as a pointer.
Answer:
Once it had been size_t but it was not suitable because
an index for string in ruby sometimes is valued negative
number.
On the other hand, ruby doesn't work on such that
sizeof(long) != sizeof(void*).
______________________________________________________________________
[ruby-dev:6992] ruby lacks some features of tr in Perl
Comment:
* String#tr cannot substitute "\0" but Perl's tr can.
* String#delete doesn't support like a Perl's tr///d.
% perl -le '$_ = "123456789"; tr/2-8/4-6/d; print'
14569
Answer:
The former is bug. It has been fixed.
The latter will be supported as optional arguments and
String#squeeze will be too in the later version.
It is planded that all arguments are logically conjuncted.
______________________________________________________________________
-- gotoken