[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dynamically Create Singleton Method

pachl

12/9/2006 1:08:00 PM

I'm not sure how to explain what I'm looking for so I will show you:

# this works, but not quite the functionality I need

def simulate_cgi_upload
class << cgi_file = Tempfile.new('simulate-cgi')
def content_type() 'image/jpeg' end
end
cgi_file
end


# this does NOT work, but has the functionality I need
# i.e.: dynamically defining content_type's return value using
# the argument from simulate_cgi_upload method.

def simulate_cgi_upload(mime_type = 'image/jpeg')
class << cgi_file = Tempfile.new('simulate-cgi')
def content_type() mime_type end
end
cgi_file
end

-pachl

12 Answers

dblack

12/9/2006 1:26:00 PM

0

pachl

12/9/2006 2:24:00 PM

0

On Dec 9, 6:26 am, dbl...@wobblini.net wrote:
> On Sat, 9 Dec 2006, pachl wrote:
> > I'm not sure how to explain what I'm looking for so I will show you:
>
> > # this works, but not quite the functionality I need
>
> > def simulate_cgi_upload
> > class << cgi_file = Tempfile.new('simulate-cgi')
> > def content_type() 'image/jpeg' end
> > end
> > cgi_file
> > end
>
> > # this does NOT work, but has the functionality I need
> > # i.e.: dynamically defining content_type's return value using
> > # the argument from simulate_cgi_upload method.
>
> > def simulate_cgi_upload(mime_type = 'image/jpeg')
> > class << cgi_file = Tempfile.new('simulate-cgi')
> > def content_type() mime_type end
> > end
> > cgi_file
> > endTry this (or something like it in case I've mistyped anything):
>
> def simulate_cgi_upload(mime_type = 'image/jpeg')
> cgi_file = Tempfile.new('simulate-cgi')
> (class << cgi_file; self; end).class_eval do
> define_method("content_type") { mime_type }
> end
> cgi_file
> end

Excactly what I was looking for; compact and fairly elegant.

Thanks for the very quick response David and good morning to you.

-pachl

Vincent Fourmond

12/9/2006 3:23:00 PM

0

pachl wrote:
> I'm not sure how to explain what I'm looking for so I will show you:
>
> # this works, but not quite the functionality I need
>
> def simulate_cgi_upload
> class << cgi_file = Tempfile.new('simulate-cgi')
> def content_type() 'image/jpeg' end
> end
> cgi_file
> end
>
>
> # this does NOT work, but has the functionality I need
> # i.e.: dynamically defining content_type's return value using
> # the argument from simulate_cgi_upload method.
>
> def simulate_cgi_upload(mime_type = 'image/jpeg')
> class << cgi_file = Tempfile.new('simulate-cgi')
> def content_type() mime_type end
> end
> cgi_file
> end

Well, you need a closure, which is not the case in what you would
like. The following code seems to do what you're looking for, just adapt it:

def a(a)
b = Object.new
c = Module.new
c.send(:define_method,:a) do # needed as define_method is private
return a
end
b.extend(c)
end

a = a('biniou')
p a.a # -> "biniou"

Cheers,

Vince

--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Gregory Seidman

12/9/2006 5:05:00 PM

0

On Sat, Dec 09, 2006 at 10:10:09PM +0900, pachl wrote:
} I'm not sure how to explain what I'm looking for so I will show you:
}
} # this works, but not quite the functionality I need
}
} def simulate_cgi_upload
} class << cgi_file = Tempfile.new('simulate-cgi')
} def content_type() 'image/jpeg' end
} end
} cgi_file
} end
}
}
} # this does NOT work, but has the functionality I need
} # i.e.: dynamically defining content_type's return value using
} # the argument from simulate_cgi_upload method.
}
} def simulate_cgi_upload(mime_type = 'image/jpeg')
} class << cgi_file = Tempfile.new('simulate-cgi')
} def content_type() mime_type end
} end
} cgi_file
} end

For what I think you're trying to do, try this:

def simulate_cgi_upload(mime_type = 'image/jpeg')
cgi_file = Tempfile.new('simulate-cgi')
(class << cgi_file; self; end).send(:attr_accessor, :content_type)
cgi_file.content_type = mime_type
cgi_file
end

} -pachl
--Greg


dblack

12/9/2006 5:39:00 PM

0

Gerard

3/23/2012 1:00:00 PM

0

Rene Gagnaux <rg_02_news@renegagnaux.ch> typed:
> On Fri, 23 Mar 2012 06:57:47 -0400, "John Wiser" <ceeclef@gmail.com>
> wrote:
>
> > Die Schei?kopf hat recht, dieses mal.
>
> Es heisst *Der* ...kopf ;-)
>

Sure.
Even name calling is no longer what it has been in the past.

Matthew B. Tepper

3/23/2012 2:31:00 PM

0

Rene Gagnaux <rg_02_news@renegagnaux.ch> appears to have caused the following
letters to be typed in news:djqom7dd9di1v6l0kpig9g1gbg6tjv5793
@renegagnaux.ch:

> On Fri, 23 Mar 2012 06:57:47 -0400, "John Wiser" <ceeclef@gmail.com>
> wrote:
>
>>Die Schei?kopf hat recht, dieses mal.
>
> Es heisst *Der* ...kopf ;-)
>
> Salut
> Ren?

I am reminded of the following line from "The Private Life of Sherlock
Holmes":

"Unter *den* Schloss."

--
Matthew B. Tepper: WWW, science fiction, classical music, ducks!!
Read about "Proty" here: http://home.earthlink.net/~oy/...
To write to me, do for my address what Androcles did for the lion
Opinions expressed here are not necessarily those of my employers.

td

3/23/2012 6:55:00 PM

0

On Mar 23, 7:37 am, "Gerard" <ghendriksen_nosp...@hotmail.com> wrote:
> td <tomdedea...@mac.com> typed:
>
>
>
>
>
>
>
>
>
> > On Mar 23, 5:57 am, "Gerard" <ghendriksen_nosp...@hotmail.com> wrote:
> > > td <tomdedea...@mac.com> typed:
>
> > > > On Mar 23, 1:06 am, Tassilo <david7ga...@aol.com> wrote:
> > > > > FWIW, the Casadesus/Rosbaud recording of Beethoven's Emperor
> > > > > Concerto has just been reissued on the label Newton Classics. I
> > > > > don't know anything about the quality of the transfer:
>
> > > > You don't have to wonder.
>
> > > > It is a Philips master and a Philips master would have been used.
>
> > > > Jesus!
>
> > > > I wonder if you check your Rice Crispies every morning to see if
> > > > they have any dust in them.
>
> > > > TD
>
> > > The question is fully legitimate.
> > > It has happened a few times that reissues by newton classics had
> > > deficiencies. I remember the Tchaikovsky symphonies by Markevich
> > > were reported here. But there was more.
>
> > I have no knowledge of such "deficiencies", Gerard. There may, of
> > course, be pressing defects in CDs from time to time. It happened much
> > more often with LPs, I seem to recall.
>
> > But Philips would not send out a faulty mastertape to Newton. No, not
> > ever.
>
> That could be right.
> But newton classics did screw it up a few times.
> Newton classics was to blame.
> The Tchaikovsky symphonies by Markevich case probably can still be found in the
> archives.
> AFAIK they (newton classics) corrected their mistakes in a new take. I don't
> know how the buyers of the defective sets were treated (if they could get
> replacements easily).

Usually these are replaced graciously and quickly. Mistakes are made
in any product. The point is to correct them.

But this is no reason to question the "master" used by Newton. They
officially license from Universal, so the only master they would use
would come from the company owning the repertoire. The Markevich
problems seem to have been in the manufacturing, not the mastering. I
certainly have no problems with my Markevich CDs, which are Philips
products manufactured in Hannover.

TD

td

3/23/2012 6:56:00 PM

0

On Mar 23, 8:59 am, "Gerard" <ghendriksen_nosp...@hotmail.com> wrote:
> Rene Gagnaux <rg_02_n...@renegagnaux.ch> typed:
>
> > On Fri, 23 Mar 2012 06:57:47 -0400, "John Wiser" <ceec...@gmail.com>
> > wrote:
>
> > > Die Scheißkopf hat recht, dieses mal.
>
> > Es heisst *Der* ...kopf ;-)
>
> Sure.
> Even name calling is no longer what it has been in the past.


Wiser tries to be wise, as well as cute. He doesn't always succeed in
either.

TD

Gerard

3/23/2012 7:25:00 PM

0

td <tomdedeacon@mac.com> typed:
> On Mar 23, 8:59 am, "Gerard" <ghendriksen_nosp...@hotmail.com> wrote:
> > Rene Gagnaux <rg_02_n...@renegagnaux.ch> typed:
> >
> > > On Fri, 23 Mar 2012 06:57:47 -0400, "John Wiser"
> > > <ceec...@gmail.com> wrote:
> >
> > > > Die Schei?kopf hat recht, dieses mal.
> >
> > > Es heisst *Der* ...kopf ;-)
> >
> > Sure.
> > Even name calling is no longer what it has been in the past.
>
>
> Wiser tries to be wise, as well as cute. He doesn't always succeed in
> either.
>
> TD

He's even more predictable than wagner rat.