[ruby-talk:00345] FromJapan: #0

From: gotoken@... (GOTO Kentaro)
Date: 1999-05-26 07:09:42 UTC
List: ruby-talk #345
Hi, 

Well, I'll write summaries of Japanese speaking mailing lists from
time to time (I'm going to submit weekly but don't declare to be a
periodical).  It is not intended to cover all topics but to give
little tips or chances to talk about inconspicuous features here.
Also, it is not red hot because answers converge about one week after
(or because I read or understand not all messages immediately).

# Hey Japanese guys! I'll never keep a monopoly of joy to translate;
# You also can play this game :-)

Today, I pick up 10 topics -- 3 contrib and 7 Q&As. 

    ______________________________________________________________________

	Contributions
	-------------

	[ruby-list:14332] ruby/gtk 0.17

	Kand-san pre-released new version of ruby/gtk 0.17. 
	gtk+-1.2.x is supported from this version. 
	  http://www.minato.net/~j2/software/download/ruby-gtk-0.17.tar.gz
	Some problems are found when compiling with Ruby 1.2.x.
	So, 1.3.3 may be recommended to make this extended module. 
    ______________________________________________________________________

	[ruby-list:14354] eRuby 0.0.1

	Shugo put eRuby 0.0.1 into contrib. eRuby is embetting ruby 
	to HTML.  eRuby works as both an filter and a CGI. 

	See example of souce:
	  http://www.netlab.co.jp/~shugo/eruby/hello.rhtml
	and the result:
	  http://www.netlab.co.jp/cgi-bin/eruby/~shugo/eruby/hello.rhtml

	.rhtml file does not have to be set exec permission. e.g., 
	  URL of eRuby      http://your.host.name/path/to/eruby
	  URL of HTML       http://your.host.name/~user/rhtml
	  then
		http://your.host.name/path/to/eruby/~user/rhtml
	  obtains filtered source. 

	See example of debuging info
	  http://www.netlab.co.jp/cgi-bin/eruby/~shugo/eruby/stupid.rhtml
    ______________________________________________________________________

	[ruby-list:14402] mod_ruby & eRuby

	Shugo put `mod_ruby-0.1.1.tar.gz' and `eruby-0.0.2.tar.gz' into 
	contrib.  mod_ruby is Apache module as same as mod_perl. 

	The main change is mod_ruby supported eRuby

$ ./Makfile.RB --enable-eruby --eruby-dir=../eruby

	Then put the following line into `srm.conf' or etc.

AddType application/x-httpd-eruby .rhtml

	You can use eRuby as same as PHP3. 
    ______________________________________________________________________

	Q and A
	-------
	[ruby-list:14203] class def in class def

	Question:
		Why the folloing code raises error?

class Abc
  def initialize
    @a = Def.new
  end
  class Def
    def xxx;print "I'm in Def\n"; end
  end
  attr :a
end

tmp = Abc.new
tmp.a.xxx
Def.new #=> NameError


	Answer: 
		Because a name of class or module is an constant, 
		nested classes can be referred as follows:

Abc::Def.new() #=> works!


	Side effect:
		This question causes a long thread (about 50msgs). 
		In this thread the following code supplied some spice
		matters. 

class Foo
  Baz = 1; baz = 2
  class Bar
    Baz = 3; baz = 4  #=> warning: already initialized constant Baz
    p Baz, baz
  end
  p Baz, baz
end
p aFoo = Foo.new()
		
		Q: Why is Baz warned?
		A: The inner class Bar shares constants of the outer 
		   class Foo. Since the outer Baz become invisible 
		   after the substitution `Baz = 3', this is warned 
		   but not error. 

		Q: When are `p Baz, baz' evaluated?
		A: In Ruby, also a class definition is evaluated. 
		   So, these are evaluated when each classe is defined. 

		Gripe: I trapped on this feature with `-n' option. 
		   Class constants are re-initialized for each iteration. 
		Advice: Store the definitions or initializer into an file, 
		   then `require' it. How about?
		Q: ?? It just change the problem into `require' many times?
		A: `require' is never done twice.  However, why do you
		   take the trouble to use `-n' options with definitions?
    ______________________________________________________________________

	[ruby-list:14257] hajimemasite

	Question:
		What ruby code do the task as same as the code in Perl:

$\="\n";
$,=",";
while(<>){
	chop;
	split(/,/);
	print $_[0],$_[1],$_[0] + $_[1];
}

		or Awk:

BEGIN {FS=OFS=","}
{ print $1,$2,$1+$2 }
		

	Answer:
		very ruby

ARGF.each_line do |line|
  fields = line.chomp.split(/,/)
  puts [ fields[0], fields[1], fields[0].to_i + fields[1].to_i ].join(',')
end

	Answer:
		perl-like

$\ = "\n"
$, = ","
while gets
  chomp!
  fields = split(/,/)
  print fields[0], fields[1], fields[0].to_i + fields[1].to_i
end

	Answer:
		awk-like

#!/usr/local/bin/ruby -nal
  
BEGIN { $; = $, = "," }
print $F[0], $F[1], $F[0].to_i + $F[1].to_i
    ______________________________________________________________________

	[ruby-list:14282] What's |n| ?

	Question:
		Hi,
		I found the following example code in User's guide. 
		But What's |n|?

 ruby> def foo
 ruby|   i = 15
 ruby|   get = proc{i}
 ruby|   set = proc{|n| i = n}
 ruby|   return get, set
 ruby| end
 ruby> p1, p2 = foo


	Answer:
		See below instead of explanation: (`$' for bash prompt)

$ ruby -e '
def foo
  i = 15
  get = proc{i}
  set = proc{|n| i = n}
  return get, set
end
p1, p2 = foo
p p1.call
p2.call(1)  # as same as i = 1
p p1.call
'
15
1
$
    ______________________________________________________________________

	[ruby-list:14292] RUBYLIB is necessary for 1.3 ?

	Question:
		Why is not /usr/local/bin/ruby/mine.rb required?

	Answer:

% ruby-1.3 -le 'print $:.join("\n")'
/usr/local/lib/ruby/1.3/site_ruby/i386-freebsd2.2.6
/usr/local/lib/ruby/1.3/site_ruby
/usr/local/lib/ruby/1.3/i386-freebsd2.2.6
/usr/local/lib/ruby/1.3
.
% 
		That is, /usr/local/lib/ruby is no longer contained by 
		the load path list. 
    ______________________________________________________________________

	[ruby-list:14348] vec

	Question:
		What is perl's feature vec in Ruby?

	Answer:
		Well..., Integer?

bits = 0
bits |= 1<<256

	Signature of the Question:

[<<':-)'.tr(" \n", '0').gsub(/:-\)/, '1')].pack('B*').display
 :-)  :-) :-)  :-):-):-) :-) :-) :-):-):-)  :-):-) :-):-):-) :-)    :-)
     :-):-)    :-) :-):-) :-):-):-)  :-):-) :-):-):-):-) :-):-):-) :-)
  :-):-) :-)    :-):-)  :-) :-) :-):-):-)  :-)   :-)      :-) :-)  :-)
 :-):-):-) :-) :-) :-):-)   :-)  :-):-):-):-)  :-)  :-)      :-):-) :-)
   :-):-)    :-) :-):-)   :-):-) :-):-) :-) :-):-) :-):-)  :-) :-) :-):-):-)
 :-)   :-) :-):-)

:-)

    ______________________________________________________________________

	[ruby-list:14371] regexp for backslash escaped strings

	Question:
		Can I split the folloing csh string into words by a regexp?

	Answer:
		How about lib/shellwords.rb?
	Answer:
% ruby -e 'p "\\\\a a\\ b \\\\\\c".scan(/(?:[^\\\s]|\\.)+/)'
["\\\\a", "a\\ b", "\\\\\\c"]

    ______________________________________________________________________

	[ruby-list:14417]

	Question:
		Why does sub! changes date1 too?

today = "1999/05/21"
date1 = today
today.sub!(/^/, "'")
today.sub!(/$/, "'")

	Answer:
		The focus is not `sub!' but the substitution. 
		In ruby any substitutions (lhs = rhs) are just (re)nameing 
		the object.  And each variable appearing in rhs is 
		indicate the object which is named as the variable. 
		So, 

today = "1999/05/21"
date1 = today

		causes that today and date1 point identical object as 
		follows:

today ---> "1999/05/21" <--- date1


		On the other hand, 

today = "1999/05/21"
date1 = "1999/05/21"

		make another situation, that is, today and date1
		points different objects, which have same display. 
		See the following diagram. 

today ---> "1999/05/21"
date1 ---> "1999/05/21"

		Now, I mind two solutions of the problem. 

		1. sub

today = "1999/05/21"
date1 = today
today = today.sub(/^/, "'")
today = today.sub(/$/, "'")

		Remark: `str = str.sub(...)' has distinct semantics
		than `str!.sub(...)'; the former changes what object 
		str points whareas str.sub! does not change what str 
		is pointed. 

		2. dup

today = "1999/05/21"
date1 = today.dup
today.sub!(/^/, "'")
today.sub!(/$/, "'")

		dup returns new clone of the object. 

    ______________________________________________________________________

-- gotoken

In This Thread

Prev Next