tag:blogger.com,1999:blog-12467669.post7338004104465237791..comments2023-04-29T07:23:25.825-04:00Comments on Jay Fields' Thoughts: Ruby: Creating Anonymous ClassesJayhttp://www.blogger.com/profile/14491442812573747680noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-12467669.post-49583945236613145732011-12-05T10:43:54.647-05:002011-12-05T10:43:54.647-05:00You could dynamically set the variable to a consta...You could dynamically set the variable to a constant to set a name:<br /><br />ruby-1.8.7-p352 :001 > module Bar; end<br />ruby-1.8.7-p352 :002 > t = Class.new {}<br />ruby-1.8.7-p352 :003 > Bar.const_set('Jabberwocky', t)<br />ruby-1.8.7-p352 :004 > t.name<br /> => "Bar::Jabberwocky"Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-82017637240611521192011-12-05T10:43:30.066-05:002011-12-05T10:43:30.066-05:00You could dynamically set the variable to a consta...You could dynamically set the variable to a constant to set a name:<br /><br />ruby-1.8.7-p352 :001 > module Bar; end<br />ruby-1.8.7-p352 :002 > t = Class.new {}<br />ruby-1.8.7-p352 :003 > Bar.const_set('Jabberwocky', t)<br />ruby-1.8.7-p352 :004 > t.name<br /> => "Bar::Jabberwocky"Tysanahttps://www.blogger.com/profile/08925375117601571411noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-21227836183356024112010-11-06T10:02:20.387-04:002010-11-06T10:02:20.387-04:00The major problem with Class.new is can't spec...The major problem with Class.new is can't specify a class name so you have no way of referencing it later. <br /><br />You might say, "Well who cares, you've got a variable that references the class." The issue is I need the class name because I'm doing dynamic class generation testing. I need to store the class name so I can test an actual class#method invocation as it will be used in the wild. Not being able to specify the class name is very annoying.Pickle Pumperhttps://www.blogger.com/profile/18261912735965840825noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-81312931491758489052009-03-09T04:31:00.000-04:002009-03-09T04:31:00.000-04:00irb(main):001:0> foo = Class.new=> #<Clas...irb(main):001:0> foo = Class.new<BR/>=> #<Class:0xb7cf9650><BR/>irb(main):002:0> foo.new<BR/>=> #<#<Class:0xb7cf9650>:0xb7cf3138><BR/>irb(main):003:0> Dink = foo<BR/>=> Dink<BR/>irb(main):004:0> foo.new<BR/>=> #<Dink:0xb7ce99a8><BR/>irb(main):005:0> Donk = foo<BR/>=> Dink<BR/>irb(main):006:0> Donk.new<BR/>=> #<Dink:0xb7ce2cd4><BR/><BR/><BR/>Weird .. I wonder where the mapping between class instance and constant name is kept?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-19431960072140823592008-02-25T10:34:00.000-05:002008-02-25T10:34:00.000-05:00Hi Rubikitch,I love extend. For this example it wo...Hi Rubikitch,<BR/><BR/>I love extend. For this example it would work. This is the problem with example code, you have to be concise enough that you make the point, but not so concise that it appears a simpler solution will do. When I'm not using a contrived example I usually need to include a module and do something else, such as call a class method.<BR/><BR/>For exmaple,<BR/><BR/>klass = Class.new do<BR/> attr_accessor :name<BR/> include Validatable<BR/> validate_presence_of :name<BR/>end<BR/><BR/>But, I actually like when people point out that for simple cases there are simpler solutions. It helps keep me from over-engineering.<BR/><BR/>Thanks for the example, Cheers, JayAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-7182259714516817532008-02-25T05:47:00.000-05:002008-02-25T05:47:00.000-05:00Do you like extend? class GatewayTest def test_...Do you like extend?<BR/><BR/> class GatewayTest < Test::Unit::TestCase<BR/> def test_post_returns_true<BR/> obj = Object.new.extend Gateway<BR/> assert_equal true, obj.post(1)<BR/> end<BR/> endAnonymousnoreply@blogger.com