話題の Big Scripting Languages チートシートの Smalltalk の空欄を埋めてみた [後半]

Smalltalk が普通に入っているのがすばらしいので、すこしだけ目立ち気味の空欄を GNU SmalltalkSqueakVisualWorks で項目を分けてからざっと調べて分かる範囲で埋めてみた―の巻の後半部分です。GSTer、VWer は、訂正・補足をお願いします。もちろん Squeak使いからのツッコミも歓迎です。



libraries and modules
> ruby GNU Smalltalk Squeak VisualWorks
import library require PackageLoader fileInPackage: 'PackageName'
PackageLoader fileInPackages: #('Package1' 'Package2')
use SqueakMap Package Loader or drag’n drop package file to desktop or
(Installer file: 'path/to/package/file') install
use Parcel Manager and Load Parcel Named... command or ParcelManag
library path $: ?? SMSqueakMap default directory UISettings preferenceFor: #parcelPath
library path environment variable RUBYLIB SMALLTALK_MODULES none none
library path command line option -I none, use gst-load script none -filein
module declaration class Foo or module Foo write package.xml and use gst-package script use Monticello Store.Registry packageNamedOrCreate: 'Foo'
module separator :: . (period) none . (period)
package management $ gem list
$ gem install rails
PackageLoader SMSqueakMap, Monticello ParcelManager, StORE, Monticello
objects
> ruby GNU Smalltlk Squeak VisualWorks
define class class Int
  attr_accessor :value
  def initialize(i=0)
    @value = i
  end
end
Object subclass: Int [
  | val |
  initialize [
    val := 0
  ]
  val [^val]
  val: i [val := i]
  Int class >> new [
    ^super new initialize; yourself
  ]
  Int class >> new: i [
    ^self new val: i; yourself
  ]
]
in class browser
Object subclass: #Int
  instanceVariableNames: 'val'
  classVariableNames: ''
  poolDictionaries: ''
  category: 'Category-Name'

use browser’s "create inst var accessors" command

Int >> initialize
  super initialize
  val := 0

Int clas >> new: i
  ^self new val: i; yourself
use class browser’s New Class...   dialog and set Create Methods: options for creating isnt var asscessors

in class browser

Int >> initialize
  super initialize.
  val := 0.
  ^self

Int clas >> new: i
  ^self new initialize val: i; yourself
create object i = Int.new
i = Int.new(4)
i := Int new
i := Int new: 4
i := Int new
i := Int new: 4
i := Int new
i := Int new: 4
invoke getter, setter v = i.value
i.value = v+1
v := i val.
i val: v + 1
v := i val.
i val: v + 1
v := i val.
i val: v + 1
instance variable accessibility private by default; use attr_reader, attr_writer, attr_accessor to make public private by default; create accessors to make public private by default; create accessors to make public private by default; create accessors to make public
define method in class body:
def plus(i)
  value + i
end
Int extend [
  plus: i [^val + i]
]
in class browser:

Int >> plus: i
  ^val + i
in class browser:

Int >> plus: i
  ^val + i
invoke method i.plus(7) i plus: 7 i plus: 7 i plus: 7
cascading none i val: 4; plus: 7 i val: 4; plus: 7 i val: 4; plus: 7
destructor val = i.value
ObjectSpace.define_finalizer(i) {
  puts "bye, #{val}"
}
i addToBeFinalized

define finalize method beforehand

Int extend [
  finalize [
  ('bye, ', val printString) displayNl.
  super finalize]
]
WeakRegistry default add: i

define finalize method beforehand

Int >> finalize
  Transcript cr; show: 'bye, ', val printString.
  super finalize

weakly := i withLastRites: [:obj | Transcript show: 'bye, ', obj val printString; cr]
method missing in class body:
def method_missing(name, *a)
  puts "no def: #{name}" +
    " arity: #{a.size}"
end
Int extend [
  doesNotUnderstand: msg [
  | sel args |
  sel := msg selector.
  args := msg arguments.
  ('no def: ', sel, ' args: ', args printString) displayNl]
]
Int >> doesNotUnderstand: msg
  Transcript cr; show: (
    'no def: {1} args: {2}' format:
      {msg selector. msg arguments})
Int >> doesNotUnderstand: msg
  | sel args |
  sel := msg selector.
  args := msg arguments.
  Transcript cr; show: ('no def: <1s> args: <2p>'
    expandMacrosWith: sel with: args)
inheritance class Counter < Int
  @@instances = 0
  def initialize
    @@instances += 1
    super
  end
  def incr
    self.value += 1
  end
  def self.instances
    @@instances
  end
end
Int subclass: Counter [
  incr [val := val + 1]
]

Counter class extend [
  Insts := 0.
  insts [^Insts]
]

Counter extend [
  initialize [
    super initialize.
    Insts := Insts + 1
  ]
]
use class browser
Int subclass: #Counter
  instanceVariableNames: ''
  classVariableNames: 'Insts'
  poolDictionaries: ''
  category: 'Category-Name'

Counter class >> initialize
  Insts := 0

Counter class >> insts
  ^Insts

Counter >> initialize
  super initialize.
  Insts := Insts + 1

Counter >> incr
  val := val + 1

don’t forget to invoke the class initializer
Counter initialize
use class browser’s New Class...  dialog and create a shared variable Insts in Shared Variable tab

in class browser

Count class >> initialize
  Insts := 0

Counter class >> insts
  ^Insts

Counter >> initialize
  super initialize.
  Insts := Insts + 1.
  ^self

Counter >> incr
  val := val + 1

don’t forget to invoke the class initializer
Counter initialize
invoke class method Counter.instances Counter insts Counter insts Counter insts
reflection and hooks
> ruby GNU Smalltalk Squeak VisualWorks
class a.class a class a class a class
has method? a.respond_to?('reverse') a respondsTo: #reverse a respondsTo: #reverse a respondsTo: #reverse
message passing (1..9).each { |i| a.send("phone#{i}=", nil) } (1 to: 9) do: [:i |
  a perform: ('phone', i printString, ':') asSymbol with: nil]
(1 to: 9) do: [:i |
  a perform: ('phone', i printString, ':') asSymbol with: nil]
(1 to: 9) do: [:i |
  a perform: ('phone', i printString, ':') asSymbol with: nil]
eval loop do
  puts eval(gets)
end
[Object
  evaluate: stdin nextLine, ' displayNl'
  ifError: []] repeat
[Transcript cr; show:
  (Compiler evaluate:
    (FillInTheBlank request: ''))
] repeat


[Transcript cr; show:
  (Compiler evaluate:
    (Dialog request: ''))
] repeat
methods a.methods a class selectors a class selectors a class selectors
attributes a.instance_variables a class instVarNames a class instVarNames a class instVarNames
pretty print require 'pp'
h = { 'foo'=>1, 'bar'=>[2,3] }
pp h
none none none
source line number and file name __LINE__
__FILE__
(Integer >> #factorial) methodSourcePos
(Integer >> #factorial) methodSourceFile

you can also get method source code
(Counter >> #incr) methodSourceString
(Integer >> #factorial) fileIndex
(Integer >> #factorial) filePosition

you can also get method source code
(Counter >> #incr) getSource
(Integer compiledMethodAt: #factorial) fileIndex
(Integer compiledMethodAt: #factorial) filePosition

you can also get method source code
(Integer compiledMethodAt: #factorial) getSource
web
ruby GNU Smalltalk Squeak VisualWorks
http get require 'net/http'
h = 'www.google.com'
r = Net::HTTP.start(h, 80) do |f|
  f.get('/')
end
r.body
h := 'www.google.com:80'.
r := HTTPSocket httpGetDocument: h.
r contents
url encode/decode require 'cgi'
CGI::escape('hello world')
CGI::unescape('hello+world')
none
create repo and start server $ rails foo
$ cd foo
$ ./script/server
WAKom startOn: 8888.

WAComponent subclass: #Foo
  instanceVariableNames: ''
  classVariableNames: ''
  poolDictionaries: ''
  category: 'Seaside-App'

Foo registerAsApplication: 'foo'
web framework Rails Seaside Seaside Seaside
object relational mapper ActiveRecord DBI GLORP GLORP
templates ERB ?? ?? ??
java interoperation
ruby GNU Smalltalk Squeak VisualWorks
version JRuby 1.4.0 none JNIPort, JavaConnect JNIPort, JavaConnect
repl jirb none, use a Workspace none, use a Workspace
interpreter jruby none, use a Workspace none, use a Workspace
compiler jrubyc none, use a Workspace none, use a Workspace
prologue none none none
new rnd = java.util.Random.new rnd := (JVM current findClass: 'java.util.Random') new rnd := (JVM current findClass: 'java.util.Random') new_null.
method rnd.next_float rnd nextFloat rnd nextFloat_null
import java_import java.util.Random
rnd = Random.new
none none
non-bundled java libraries require 'path/to/mycode.jar' none none
shadowing avoidance module JavaIO
  include_package "java.io"
end
none none
to java array [1,2,3].to_java(Java::int) JavaIntArray from: #(1 2 3) jvm: JVM current JavaIntArray from: #(1 2 3) jvm: JVM current
java class subclassable? yes no no
java class open? yes no no
_______________________ _______________________ _______________________ _______________________


partially copied from http://hyperpolyglot.wikidot.com/scripting then modified by sumim
and also licensed under Creative Commons Attribution-ShareAlike 3.0