Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Kernel#eval and class definition

Reply
Thread Tools

Kernel#eval and class definition

 
 
Bertram Scharpf
Guest
Posts: n/a
 
      01-11-2005
Hi,

I try to understand more deeply what Ruby does.
Why here is the first line forbidden, the second allowed?

def f ; class C ; end ; end
def f ; eval "class C ; end", binding ; end

Thanks for your answers in advance.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-12-2005

"Bertram Scharpf" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)-scharpf...
> Hi,
>
> I try to understand more deeply what Ruby does.
> Why here is the first line forbidden, the second allowed?
>
> def f ; class C ; end ; end


This is a syntax error. The construct "class ... end" is simply
syntactically not allowed in a method. IMHO it doesn't make much sense
also, but that's another story.

> def f ; eval "class C ; end", binding ; end


Well eval "..." is just an expression and "eval" evaluates everyhing it
get's that's syntactically correct. The ruby code in the string
expression is not syntactically located in method f. It's parsed at
runtime while the line above is completely evaluated at compile time.

HTH

robert


> Thanks for your answers in advance.
>
> Bertram
>
> --
> Bertram Scharpf
> Stuttgart, Deutschland/Germany
> http://www.bertram-scharpf.de
>
>


 
Reply With Quote
 
 
 
 
Yukihiro Matsumoto
Guest
Posts: n/a
 
      01-12-2005
Hi,

In message "Re: Kernel#eval and class definition"
on Wed, 12 Jan 2005 18:26:23 +0900, "Robert Klemme" <(E-Mail Removed)> writes:

|> def f ; class C ; end ; end
|> def f ; eval "class C ; end", binding ; end
|
|Well eval "..." is just an expression and "eval" evaluates everyhing it
|get's that's syntactically correct. The ruby code in the string
|expression is not syntactically located in method f. It's parsed at
|runtime while the line above is completely evaluated at compile time.

Thanks for explanation. class definition in the method is prohibited
by syntax just to detect errors. And class definition in eval is
allowed as a back door. Besides, scope management also is a reason,
but it's an implementation matter.

matz.


 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      01-12-2005
Hi,

Am Mittwoch, 12. Jan 2005, 18:43:42 +0900 schrieb Yukihiro Matsumoto:
> In message "Re: Kernel#eval and class definition"
> on Wed, 12 Jan 2005 18:26:23 +0900, "Robert Klemme" <(E-Mail Removed)> writes:
>
> |> def f ; class C ; end ; end
> |> def f ; eval "class C ; end", binding ; end
> |
> |The ruby code in the string
> |expression is not syntactically located in method f.
>
> class definition in the method is prohibited
> by syntax just to detect errors.


Ah, thank you both.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de


 
Reply With Quote
 
Florian Gross
Guest
Posts: n/a
 
      01-12-2005
Yukihiro Matsumoto wrote:

> |> def f ; class C ; end ; end
> |> def f ; eval "class C ; end", binding ; end
> |
> |Well eval "..." is just an expression and "eval" evaluates everyhing it
> |get's that's syntactically correct. The ruby code in the string
> |expression is not syntactically located in method f. It's parsed at
> |runtime while the line above is completely evaluated at compile time.
>
> Thanks for explanation. class definition in the method is prohibited
> by syntax just to detect errors. And class definition in eval is
> allowed as a back door. Besides, scope management also is a reason,
> but it's an implementation matter.


There's also Class.new which will create an anonymous class. You can
assign that to a constant via Module#const_set if you need to have a
named class for some reason. That might be a bit better than using
eval() depending on the use case.
 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      01-12-2005
Florian Gross wrote:
> Yukihiro Matsumoto wrote:
>
>> |> def f ; class C ; end ; end
>> |> def f ; eval "class C ; end", binding ; end
>> |
>> |Well eval "..." is just an expression and "eval" evaluates everyhing it
>> |get's that's syntactically correct. The ruby code in the string
>> |expression is not syntactically located in method f. It's parsed at
>> |runtime while the line above is completely evaluated at compile time.
>>
>> Thanks for explanation. class definition in the method is prohibited
>> by syntax just to detect errors. And class definition in eval is
>> allowed as a back door. Besides, scope management also is a reason,
>> but it's an implementation matter.

>
>
> There's also Class.new which will create an anonymous class. You can
> assign that to a constant via Module#const_set if you need to have a
> named class for some reason. That might be a bit better than using
> eval() depending on the use case.


Better, in the sense that it takes a block (see below). But evaling a
string to define the body of the class allows you to interpolate.

def f(arg)
Class.new do
define_method :g do arg end
end
end

puts f("hello").new.g # ==> hello


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-13-2005

"Joel VanderWerf" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Florian Gross wrote:
> > Yukihiro Matsumoto wrote:
> >
> >> |> def f ; class C ; end ; end
> >> |> def f ; eval "class C ; end", binding ; end
> >> |
> >> |Well eval "..." is just an expression and "eval" evaluates everyhing

it
> >> |get's that's syntactically correct. The ruby code in the string
> >> |expression is not syntactically located in method f. It's parsed at
> >> |runtime while the line above is completely evaluated at compile

time.
> >>
> >> Thanks for explanation. class definition in the method is prohibited
> >> by syntax just to detect errors. And class definition in eval is
> >> allowed as a back door. Besides, scope management also is a reason,
> >> but it's an implementation matter.

> >
> >
> > There's also Class.new which will create an anonymous class. You can
> > assign that to a constant via Module#const_set if you need to have a
> > named class for some reason. That might be a bit better than using
> > eval() depending on the use case.

>
> Better, in the sense that it takes a block (see below). But evaling a
> string to define the body of the class allows you to interpolate.
>
> def f(arg)
> Class.new do
> define_method :g do arg end
> end
> end
>
> puts f("hello").new.g # ==> hello


You can even use the def syntax:

irb(main):001:0> cl = Class.new do
irb(main):002:1* def foo() "bar" end
irb(main):003:1> end
=> #<Class:0x10186208>
irb(main):004:0> cl.new.foo
=> "bar"

That's better because then you can define methods that deal with blocks,
which you can't with define_method:

irb(main):005:0> cl = Class.new do
irb(main):006:1* def foo() yield "bar" end
irb(main):007:1> end
=> #<Class:0x10172468>
irb(main):008:0> cl.new.foo {|x| p x}
"bar"
=> nil

Kind regards

robert

 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      01-13-2005
Robert Klemme schrieb:
> "Joel VanderWerf" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
>>Better, in the sense that it takes a block (see below). But evaling a
>>string to define the body of the class allows you to interpolate.
>>
>>def f(arg)
>> Class.new do
>> define_method :g do arg end
>> end
>>end
>>
>>puts f("hello").new.g # ==> hello

>
>
> You can even use the def syntax:
>
> irb(main):001:0> cl = Class.new do
> irb(main):002:1* def foo() "bar" end
> irb(main):003:1> end
> => #<Class:0x10186208>
> irb(main):004:0> cl.new.foo
> => "bar"
>
> That's better because then you can define methods that deal with blocks,
> which you can't with define_method:
>
> irb(main):005:0> cl = Class.new do
> irb(main):006:1* def foo() yield "bar" end
> irb(main):007:1> end
> => #<Class:0x10172468>
> irb(main):008:0> cl.new.foo {|x| p x}
> "bar"
> => nil


The def syntax lets you define methods with blocks, but it doesn't let you use
objects from the defining scope, as Joel has been doing in his example. Note
that he passes an object into the defining method and accesses this very object
from the new class. You can't do that with the def syntax AFAIK.

Regards,
Pit


 
Reply With Quote
 
ts
Guest
Posts: n/a
 
      01-13-2005
>>>>> "R" == Robert Klemme <(E-Mail Removed)> writes:

R> That's better because then you can define methods that deal with blocks,
R> which you can't with define_method:

This is because you use strange name like #foo and "bar"and ruby don't
like it


uln% cat b.rb
#!./ruby
cl = Class.new do
define_method(:alpha) do |&block|
block["beta"]
end
end

cl.new.alpha {|x| p x}
uln%

uln% b.rb
"beta"
uln%


Guy Decoux


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-13-2005

"ts" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> >>>>> "R" == Robert Klemme <(E-Mail Removed)> writes:

>
> R> That's better because then you can define methods that deal with

blocks,
> R> which you can't with define_method:
>
> This is because you use strange name like #foo and "bar"and ruby don't
> like it


Must be a new version of Ruby that doesn't like #foo and #bar - my 1.8.1
has no problems with them. )

> uln% cat b.rb
> #!./ruby
> cl = Class.new do
> define_method(:alpha) do |&block|
> block["beta"]
> end
> end
>
> cl.new.alpha {|x| p x}
> uln%
>
> uln% b.rb
> "beta"
> uln%


Is this Ruby 1.9?

robert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decralation of class inside other class and definition outside this class =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 2 07-12-2007 11:52 PM
Automagic determination of definition based on definition location. Jon Slaughter C++ 4 10-26-2005 05:00 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments