Hi all,
can somebody tell me if a class method can call (or use) a instance method ?
Example :
class Foo
@@count = 0
def bar
@@count = @@count + 1
end
def Foo.suck
bar
end
end
By
···
-----------
Kurk
Hi all,
can somebody tell me if a class method can call (or use) a instance method ?
Example :
class Foo
@@count = 0
def bar
@@count = @@count + 1
end
def Foo.suck
bar
end
end
By
-----------
Kurk
WHICH instance of Foo would you expect it to call?
On Sun, 2004-07-18 at 01:12, Kurk Lord wrote:
can somebody tell me if a class method can call (or use) a instance method ?
Kurk Lord wrote:
Hi all,
can somebody tell me if a class method can call (or use) a instance method ?
Example :class Foo
@@count = 0def bar
@@count = @@count + 1
enddef Foo.suck
bar
end
end
You could arrange the "bar" instance method so that it calls a class method
to do the class-specific operation, making it available from either level.
class Foo
@@count = 0
def Foo.suck
bar
end
def Foo.bar
@@count = @@count + 1
# other (bar) class stuff
p [:count, @@count]
end
def bar
Foo.bar
# other (bar) instance stuff
p ['#bar', @@count]
end
end
Foo.suck #-> [:count, 1]
Foo.suck #-> [:count, 2]
Foo.new.bar #-> [:count, 3] #-> ["#bar", 3]
:daz
What's the meaning of () outside of being function parameter delimiters aka in a Lispy sense?
e.g.
(puts "command=" + (command="vi " + filename))
Obvious + is actual a method, so that explains the () around "command=", but there is the () around the entire line.
Thanks,
Nick
Thanks for all guys,
it works thanks to you!
----------------
kurk
"Kurk Lord" <kurk_lord@nospam.fr.yahoo> a écrit dans le message de
news:40f9a351$0$1906$636a15ce@news.free.fr...
Hi all,
can somebody tell me if a class method can call (or use) a instance method
?
Example :
class Foo
@@count = 0def bar
@@count = @@count + 1
enddef Foo.suck
bar
end
endBy
-----------
Kurk
Sorry, hit "send" too fast.
Extended answer to your question is as follows:
Class Foo in your example is an object (a constant, actually), which has
the methods defined as Foo.method, plus methods of Class (also a
constant, superclass of Foo).
Foo.new is more or less a factory method that can create objects with
some behaviors grouped together within class Foo / end block.
Objects created by Foo.new know who created them (Foo.new.class == Foo),
but Foo itself doesn't implicitly remember the objects it creates.
So, method like your Foo.suck can call an instance method only if it
obtains a reference to that instance from somewhere. E.g.,
class Foo
def initialize
@@lastCreated = self
end
def bar
# do something
end
def Foo.suck
(@@lastCreated || Foo.new).bar
# ^ ^
# Need to know which instance to call
end
end
Also worth noting, you cannot call private instance methods from within
class methods in Ruby (in Java you can).
Best regards,
Alexey Verkhovsky
On Sun, 2004-07-18 at 01:17, Alexey Verkhovsky wrote:
On Sun, 2004-07-18 at 01:12, Kurk Lord wrote:
> can somebody tell me if a class method can call (or use) a instance method ?WHICH instance of Foo would you expect it to call?
First, there is the expression grouping. That is what lets you say (23 + 42) * 69, or (Math.sqrt 23) + 42, or even ("this" + "that").display
Then, function parameter delimeters. These are, of course, mostly optional in ruby. STDERR.puts("Yoiks!", "Stop that!")
Parens can also act as statement groupings. Multiple statements can be collected into one expression, where the value of the expression -- everything inside the parens -- is given the value of the last statement. So, you can do things like this (warning! very contrived):
baz = ((foo=23; bar=foo+7; bar*foo) if condition)
In this, if condition is true, foo is set to 23, bar is set to 30, and the parenthetical expression returns a value of 23*30. baz will be set to 690. If condition is false, nothing else is evaluated, and baz is set to nil.
I hope the last part wasn't more confusing than helpful...
HTH,
Mark
On Jul 17, 2004, at 8:45 PM, Nicholas Van Weerdenburg wrote:
What's the meaning of () outside of being function parameter delimiters aka in a Lispy sense?
e.g.
(puts "command=" + (command="vi " + filename))Obvious + is actual a method, so that explains the () around "command=", but there is the () around the entire line.
Nicholas Van Weerdenburg wrote:
What's the meaning of () outside of being function parameter delimiters aka in a Lispy sense?
e.g.
(puts "command=" + (command="vi " + filename))
Well, they help define evaluation order for expressions.
Ruby makes few distinctions between statements and expressions. Or,
maybe more precisely, pretty much everything in Ruby is an expression,
and so most things return a value. (I'm tempted to say that
*everything* returns a value, but I'm not positive about that. Can someone clarify this?)
Many of the return values are silently discarded, though you'll see them
if you use irb.
If you tend to chain things together, the parens can help ensure the
execution and evaluation occurs as you intended.
You can also use them to mimic Lisp syntax, then egg people on in a
thread abut Ruby and functional programming.
(def cons s1, s2
"#{s2}#{s1}" end )
(def foo x
( cons( ( x ), ("Foo has ") ) ) end )
(def bar y
( cons( ( y ), ("Bar has ") ) ) end )
( puts( foo( bar( "baz" ) ) ) )
James
Hello,
I am confused about a feature of the Ruby debugger. The debugger documentation lists all the
commands you can use, such as 'w' for displaying the stack frame, or 'l' for looking
at the source. These commands work the way I expected.
The "display variables" commands, however, aren't doing what I expect... The documentation lists
them like so:
v[ar] g[lobal] show global variables
v[ar] l[ocal] show local variables
...but when I type 'v g', I get the error message:
1undefined local variable or method `g' for main:Object
What I was hoping was that these commands would list the global and local variables (perhaps with
their current values). I know that I can type the actual symbol names and get the values that way, but
a listing of all the variables would be very handy. Am I missing something?
Thanks for any help! If there is a forum or mailing list more suited for newby questions such as this,
please let me know!
Jay
I think "statement grouping" clears it up for me. I guess in another way, () act as a form of block- you could even think of method parameters as another type of block being passed to a method. {} is syntactic sugar to define a second block being passed as a higher-order function/closure (with the additional parameter passing/yield capability).
{|x| do x} --> (lamba (|x| do x)) or something like that.
Thanks,
Nick
Mark Hubbart wrote:
On Jul 17, 2004, at 8:45 PM, Nicholas Van Weerdenburg wrote:
What's the meaning of () outside of being function parameter delimiters aka in a Lispy sense?
e.g.
(puts "command=" + (command="vi " + filename))Obvious + is actual a method, so that explains the () around "command=", but there is the () around the entire line.
First, there is the expression grouping. That is what lets you say (23 + 42) * 69, or (Math.sqrt 23) + 42, or even ("this" + "that").display
Then, function parameter delimeters. These are, of course, mostly optional in ruby. STDERR.puts("Yoiks!", "Stop that!")
Parens can also act as statement groupings. Multiple statements can be collected into one expression, where the value of the expression -- everything inside the parens -- is given the value of the last statement. So, you can do things like this (warning! very contrived):
baz = ((foo=23; bar=foo+7; bar*foo) if condition)
In this, if condition is true, foo is set to 23, bar is set to 30, and the parenthetical expression returns a value of 23*30. baz will be set to 690. If condition is false, nothing else is evaluated, and baz is set to nil.
I hope the last part wasn't more confusing than helpful...
HTH,
Mark
Thanks. The code example is enlightening.
Nick
James Britt wrote:
Nicholas Van Weerdenburg wrote:
What's the meaning of () outside of being function parameter delimiters aka in a Lispy sense?
e.g.
(puts "command=" + (command="vi " + filename))Well, they help define evaluation order for expressions.
Ruby makes few distinctions between statements and expressions. Or,
maybe more precisely, pretty much everything in Ruby is an expression,
and so most things return a value. (I'm tempted to say that
*everything* returns a value, but I'm not positive about that. Can someone clarify this?)Many of the return values are silently discarded, though you'll see them
if you use irb.If you tend to chain things together, the parens can help ensure the
execution and evaluation occurs as you intended.You can also use them to mimic Lisp syntax, then egg people on in a
thread abut Ruby and functional programming.(def cons s1, s2
"#{s2}#{s1}" end )(def foo x
( cons( ( x ), ("Foo has ") ) ) end )(def bar y
( cons( ( y ), ("Bar has ") ) ) end )( puts( foo( bar( "baz" ) ) ) )
James
Tonight I fooled around with the debugger, and I found that the v[ar] g[lobal] and v[ar] l[ocal]
commands work fine with my old version of Ruby that came with my machine (1.6.8), but they don't
work with the newer version that I compiled myself: 1.8.1 [powerpc-darwin]. When I try to list
variables with the debugger in version 1.8.1, I get the error message '1undefined local variable or
method `g' for main:Object'.
Anybody have any idea what's wrong? I'm running Mac OS 10.3.
Jay
On Sun, 18 Jul 2004 23:05:54 +0900, jay wrote
Hello,
I am confused about a feature of the Ruby debugger. The debugger
documentation lists all the commands you can use, such as 'w' for
displaying the stack frame, or 'l' for looking at the source. These
commands work the way I expected.The "display variables" commands, however, aren't doing what I
expect... The documentation lists them like so:v[ar] g[lobal] show global variables
v[ar] l[ocal] show local variables...but when I type 'v g', I get the error message:
1undefined local variable or method `g' for main:Object
What I was hoping was that these commands would list the global and
local variables (perhaps with their current values). I know that I
can type the actual symbol names and get the values that way, but a
listing of all the variables would be very handy. Am I missing something?Thanks for any help! If there is a forum or mailing list more suited
for newby questions such as this, please let me know!Jay
Hi,
In message "Ruby Debugger options: v[ar] g[lobal] ..." on 04/07/18, "jay" <jay@fleeingrabbit.com> writes:
The "display variables" commands, however, aren't doing what I expect... The documentation lists
them like so:v[ar] g[lobal] show global variables
v[ar] l[ocal] show local variables...but when I type 'v g', I get the error message:
1undefined local variable or method `g' for main:Object
I can't reproduce your problem. More information please.
How you compiled/installed your Ruby, the version, etc.
matz.
"Yukihiro Matsumoto" <matz@ruby-lang.org> schrieb im Newsbeitrag
news:1090312949.825866.21834.nullmailer@picachu.netlab.jp...
Hi,
>The "display variables" commands, however, aren't doing what I
expect... The documentation lists
>them like so:
>
> v[ar] g[lobal] show global variables
> v[ar] l[ocal] show local variables
>
>...but when I type 'v g', I get the error message:
>
> 1undefined local variable or method `g' for main:ObjectI can't reproduce your problem. More information please.
How you compiled/installed your Ruby, the version, etc.
Does this help?
13:26:35 [source]: uname -a
CYGWIN_NT-5.0 bond 1.5.7(0.109/3/2) 2004-01-30 19:32 i686 unknown unknown
Cygwin
13:26:53 [source]: ruby --version
ruby 1.8.1 (2003-12-25) [i386-cygwin]
13:26:57 [source]: ruby -rdebug -e '$foo="x";bar="y";1000.times {|i| puts
i }'
Debug.rb
Emacs support available.
-e:1:$foo="x";bar="y";1000.times {|i| puts i }
(rdb:1) $foo
nil
(rdb:1) s
-e:1:$foo="x";bar="y";1000.times {|i| puts i }
(rdb:1) $foo
"x"
(rdb:1) v foo
-e:1:(eval):1undefined local variable or method `foo' for main:Object
(rdb:1) v g foo
(eval):1: warning: parenthesize argument(s) for future version
-e:1:(eval):1undefined local variable or method `foo' for main:Object
(rdb:1) bar
nil
(rdb:1) v bar
-e:1:(eval):1undefined method `v' for main:Object
(rdb:1) step
-e:1:$foo="x";bar="y";1000.times {|i| puts i }
(rdb:1) bar
"y"
(rdb:1) v bar
-e:1:(eval):1undefined method `v' for main:Object
(rdb:1) q
Really quit? (y/n) y
Dunno whether this is related to readline built in or not.
Regards
robert
In message "Ruby Debugger options: v[ar] g[lobal] ..." > on 04/07/18, "jay" <jay@fleeingrabbit.com> writes:
I compiled and installed Ruby 1.8.1 with the default configuration on a G3 PowerPC running Mac OS
10.3. Everything seemed to work except there were a few weird debugger glitches, one of which was
the list variables command.
This morning I installed a pre-compiled binary in a different location, and it does not have this
problem, so I will use that for now. Thanks for your help!
On Tue, 20 Jul 2004 17:42:31 +0900, Yukihiro Matsumoto wrote
Hi,
In message "Ruby Debugger options: v[ar] g[lobal] ..." > on 04/07/18, "jay" <jay@fleeingrabbit.com> writes:
>The "display variables" commands, however, aren't doing what I
expect... The documentation lists |them like so: | | v[ar]
g[lobal] show global variables | v[ar] l[ocal]
show local variables | |...but when I type 'v g', I get the
error message: | | 1undefined local variable or method `g' for main:ObjectI can't reproduce your problem. More information please.
How you compiled/installed your Ruby, the version, etc.matz.
Dunno whether this is related to readline built in or not.
/usr/local is 1.8.1, r182 is 1.8.2
svg% diff -u /usr/local/lib/ruby/1.8/debug.rb r182/lib/ruby/1.8/debug.rb
--- /usr/local/lib/ruby/1.8/debug.rb 2003-11-28 01:15:03.000000000 +0100
+++ r182/lib/ruby/1.8/debug.rb 2004-01-15 12:36:27.000000000 +0100
@@ -507,21 +507,21 @@
exit! # exit -> exit!: No graceful way to stop threads...
end
- when /^\s*v(?:ar)?\s+$/
+ when /^\s*v(?:ar)?\s+/
debug_variable_info($', binding)
- when /^\s*m(?:ethod)?\s+$/
+ when /^\s*m(?:ethod)?\s+/
debug_method_info($', binding)
- when /^\s*th(?:read)?\s+$/
+ when /^\s*th(?:read)?\s+/
if DEBUGGER__.debug_thread_info($', binding) == :cont
prompt = false
end
- when /^\s*pp\s+$/
+ when /^\s*pp\s+/
PP.pp(debug_eval($', binding), stdout)
- when /^\s*p\s+$/
+ when /^\s*p\s+/
stdout.printf "%s\n", debug_eval($', binding).inspect
when /^\s*h(?:elp)?$/
svg%
Guy Decoux
Here is my own version of the above:
ra88it:~ jcotton$ uname -a
Darwin ra88it.local 7.4.0 Darwin Kernel Version 7.4.0: Wed May 12 16:58:24 PDT 2004; root:xnu/
xnu-517.7.7.obj~7/RELEASE_PPC Power Macintosh powerpc
ra88it:~ jcotton$ /usr/local/bin/ruby --version
ruby 1.8.1 (2003-12-25) [powerpc-darwin]
ra88it:~ jcotton$ /usr/local/bin/ruby -rdebug -e '$foo="x";bar="y";1000.times {|i| puts i }'
Debug.rb
Emacs support available.
-e:1:
(rdb:1) $foo
nil
(rdb:1) s
-e:1:
(rdb:1) $foo
"x"
(rdb:1) v foo
-e:1:(eval):1undefined local variable or method `foo' for main:Object
(rdb:1) v g foo
(eval):1: warning: parenthesize argument(s) for future version
-e:1:(eval):1undefined local variable or method `foo' for main:Object
(rdb:1) bar
nil
(rdb:1) v bar
-e:1:(eval):1undefined method `v' for main:Object
(rdb:1) step
-e:1:
(rdb:1) bar
"y"
(rdb:1) v bar
-e:1:(eval):1undefined method `v' for main:Object
(rdb:1) q
Really quit? (y/n) y
ra88it:~ jcotton$
Note: I have readline support built in to this version as well as the binary version that I installed in a
different location this morning. The binary version does not have this debugger problem, however.
Thanks,
Jay
On Tue, 20 Jul 2004 20:32:05 +0900, Robert Klemme wrote
"Yukihiro Matsumoto" <matz@ruby-lang.org> schrieb im Newsbeitrag
news:1090312949.825866.21834.nullmailer@picachu.netlab.jp...
> Hi,
>
> In message "Ruby Debugger options: v[ar] g[lobal] ..." > > on 04/07/18, "jay" <jay@fleeingrabbit.com> writes:
>
> >The "display variables" commands, however, aren't doing what I
expect... The documentation lists
> >them like so:
> >
> > v[ar] g[lobal] show global variables
> > v[ar] l[ocal] show local variables
> >
> >...but when I type 'v g', I get the error message:
> >
> > 1undefined local variable or method `g' for main:Object
>
> I can't reproduce your problem. More information please.
> How you compiled/installed your Ruby, the version, etc.Does this help?
13:26:35 [source]: uname -a
CYGWIN_NT-5.0 bond 1.5.7(0.109/3/2) 2004-01-30 19:32 i686 unknown unknown
Cygwin
13:26:53 [source]: ruby --version
ruby 1.8.1 (2003-12-25) [i386-cygwin]13:26:57 [source]: ruby -rdebug -e '$foo="x";bar="y";1000.times {|i|
puts i }' Debug.rb Emacs support available.-e:1:$foo="x";bar="y";1000.times {|i| puts i }
(rdb:1) $foo
nil
(rdb:1) s
-e:1:$foo="x";bar="y";1000.times {|i| puts i }
(rdb:1) $foo
"x"
(rdb:1) v foo
-e:1:(eval):1undefined local variable or method `foo' for main:Object
(rdb:1) v g foo
(eval):1: warning: parenthesize argument(s) for future version
-e:1:(eval):1undefined local variable or method `foo' for main:Object
(rdb:1) bar
nil
(rdb:1) v bar
-e:1:(eval):1undefined method `v' for main:Object
(rdb:1) step
-e:1:$foo="x";bar="y";1000.times {|i| puts i }
(rdb:1) bar
"y"
(rdb:1) v bar
-e:1:(eval):1undefined method `v' for main:Object
(rdb:1) q
Really quit? (y/n) yDunno whether this is related to readline built in or not.
Regards
robert