open command window
change to project directory
cmd: irb
within irb
cmd: require 'talker'
talker is created, produces output.
but: the "talker" instance which was created during read in (of
talker.rb) has died.
why?
Local variables evaluated during a require() are not accessible
from outside the file; you may consider this such that the file
is the scope within which the variable is defined, just like in
a method body.
how do i read-in files, which produce instances, which 'survive'?
Global variables (although this practice would be discouraged),
which are identified by a $-prefix, e.g. $global = 'something'.
Come to think of it, constants may also be accessible.
Normally one will have either an accessor method of some sort
or simply provide the means of _creating_ an instance.
and: how do I delete an instance? [tried: del, delete, kill, rm, free]
One cannot explicitly delete an instance. It will be 'deleted'
(marked for garbage collection) once it is without references.
E
No-one expects the Solaris POSIX implementation!
···
Le 3/4/2005, "Ilias Lazaridis" <ilias@lazaridis.com> a écrit:
Note that it is quite large and not too pretty, though.
* A deep (but compact) description of the reflection/introspection api's.
Well, the Pickaxe book (slightly outdated version is available online for free, new issue with lots of new material can be ordered online) contains that among much other information and is a pretty interesting read. See
Your comment has already hit on one solution. There may be many others, depending on how you intend to use this information...
I extract: Ruby has not standard-mechanism for metadata/annotations.
ok
Correct, but it certainly provides ample tools to build whatever you need.
This aspect you've just hit on is the very essence of programming, in my opinion. No language provides everything for every need. The processes of adapting your chosen language to the problem space is the craft.
Where can I find the implementation of "attr_accessor"?
I'm intrested in seeing how much effort it is to write an own "flex_attr_accessor".
I'm sure attr_accessor is defined in the Ruby source. Here's my version of a pure Ruby class accessor:
irb(main):015:0> class Module
irb(main):016:1> def cattr_accessor( *symbols )
irb(main):017:2> symbols.each do |sym|
irb(main):018:3* class_eval "def self.#{sym}( ) @@#{sym} end
irb(main):019:3" def self.#{sym}=( value ) @@#{sym} = value end"
irb(main):020:3> end
irb(main):021:2> end
irb(main):022:1> end
=> nil
irb(main):023:0> class Accessor
irb(main):024:1> cattr_accessor :one, :two
irb(main):025:1> def self.fetch_one( )
irb(main):026:2> @@one
irb(main):027:2> end
irb(main):028:1> end
=> nil
irb(main):029:0> Accessor.one = "James"
=> "James"
irb(main):030:0> Accessor.two = "Gray"
=> "Gray"
irb(main):031:0> Accessor.one
=> "James"
irb(main):032:0> Accessor.two
=> "Gray"
irb(main):033:0> Accessor.fetch_one
=> "James"
Hope that helps.
James Edward Gray II
···
On Mar 31, 2005, at 3:44 AM, Ilias Lazaridis wrote:
"#" is used as a comment marker _and_ partly within code.
Can I use another comment marker?
No. To my knowledge there is only the one comment syntax in ruby.
ok
Can I write "puts "#{thisMethodName}: Hello World"" in an different
manner, without the use of "#"?
Sure. #{thisMethodName} in a string literal simply calls .to_s on the
enclosed expression and inserts it at the designated location. So we
can pull that out such:
puts thisMethodName.to_s + ": Hello World"
ok.
I like this contruct more:
"puts "#{thisMethodName}: Hello World"
but the usage of "#" is missleading (comment marker)
The + operator performs concatenation when its arguments are strings.
Jacob Fugal
..
···
On Thu, 24 Mar 2005 10:59:50 +0900, Ilias Lazaridis <ilias@lazaridis.com> wrote:
how can I add runtime-accessible [meta]data to a ruby function definition?
Any standard to do this?
?
Or any suggestions?
Put a hash into the class with symbol as key and whatever meta data you need. Add some syntactic sugar and you're done. I'm quite sure someone has done that already (maybe even on RAA).
I don't understand.
def talker
def sayHello
puts "Hello World"
end
def sayYourName
puts @name
end
end
how do I put a "hash" which keeps metadata to each _function_?
class Module
def meta() @meta ||= {} end
end
class Foo
def bar() end
meta[:bar] = "bar_meta"
end
open command window
change to project directory
cmd: irb
within irb
cmd: require 'talker'
talker is created, produces output.
but: the "talker" instance which was created during read in (of
talker.rb) has died.
why?
Local variables evaluated during a require() are not accessible
from outside the file; you may consider this such that the file
is the scope within which the variable is defined, just like in
a method body.
I understand.
instead of 'require', can I use another command, like e.g. "expand".
how do i read-in files, which produce instances, which 'survive'?
Global variables (although this practice would be discouraged),
which are identified by a $-prefix, e.g. $global = 'something'.
Come to think of it, constants may also be accessible.
ok
Normally one will have either an accessor method of some sort
or simply provide the means of _creating_ an instance.
and: how do I delete an instance? [tried: del, delete, kill, rm, free]
One cannot explicitly delete an instance. It will be 'deleted'
(marked for garbage collection) once it is without references.
how can i reverse the process?
I like to kill the object, thus the destructor is called.
the references will point to nil.
..
···
Le 3/4/2005, "Ilias Lazaridis" <ilias@lazaridis.com> a écrit:
And this means that I can drop the "if __FILE__ [...]" construct.
__FILE__ is the name of the current source file.
$0 is the name of the top-level ruby program being executed.
So this construct just checks if this the source file is the file that is executed directly with e.g. 'ruby talker.rb', and only if this is the case, the codeblock will be executed.
Note that it is quite large and not too pretty, though.
This is a nice overview.
Does anyone has the detailed model of the core class-model?
* A deep (but compact) description of the reflection/introspection api's.
Well, the Pickaxe book (slightly outdated version is available online for free, new issue with lots of new material can be ordered online) contains that among much other information and is a pretty interesting read. See
Is there a standard way to apply metadata/annotations to my class Talker, its Methods, its Attributes?
[...]
I extract: Ruby has not standard-mechanism for metadata/annotations.
ok
Correct, but it certainly provides ample tools to build whatever you need.
This aspect you've just hit on is the very essence of programming, in my opinion. No language provides everything for every need. The processes of adapting your chosen language to the problem space is the craft.
I'm just writing the situation down.
personally to me, language-efficiency is more important than a standartization [which can btw. limit efficiency, see JAVA].
Where can I find the implementation of "attr_accessor"?
I'm intrested in seeing how much effort it is to write an own "flex_attr_accessor".
I'm sure attr_accessor is defined in the Ruby source.
ok
Here's my version of a pure Ruby class accessor:
irb(main):015:0> class Module
irb(main):016:1> def cattr_accessor( *symbols )
[...]
Hope that helps.
yes, very nice!
looks like a "macro", written in ruby, made available to a project by simply including the file.
very nice!
James Edward Gray II
..
···
On Mar 31, 2005, at 3:44 AM, Ilias Lazaridis wrote:
And this means that I can drop the "if __FILE__ [...]" construct.
__FILE__ is the name of the current source file.
ok, here i guessed right (intuitive naming).
$0 is the name of the top-level ruby program being executed.
here i thought that "$0" represents "NULL".
$0 => main-file (or entry-file, or top-level file, or top-level program)
So this construct just checks if this the source file is the file that is executed directly with e.g. 'ruby talker.rb', and only if this is the case, the codeblock will be executed.