3 ms·
Ah-Ha Moment: I wrote my first program with eval and lambda!
I've 'understood' what eval and lambda were for in Ruby but I hadn't ever had a real reason to use it for anything, until today.
Now I REALLY get what they are for and I can definitely see how you could use them all over the place.
Just wanted to share my Hacker Joy ;)
- nonrecursive 19y agoCan you share the code?
- Readmore 19y agoSure, I'm always nervous about showing off my sloppy code but here it is. http://pastie.caboo.se/161944 http://pastie.caboo.se/161944 It's not the craziest code ever but it allows me to store site specific indexing code in a file and then load the correct code based on the site being indexed.
- iamwil 19y agoI've been using Ruby for a while, but I've been using proc/lamba and eval sparingly. It's nice when you need it. I've mostly used proc to delay computation by passing blocks around, and eval to generate code or class_eval to write as if I was writing in the class. Anyone else use them for anything else? So, the aha moment was being able to use eval as an interpreter, and lambda to enclose it to pass it around?
- bct 19y agoAny reason you did parseClosure(x,y,lambda {|doc,out| ... }) instead of parseClosure(x,y) { |doc,out| ... } ?
- Readmore 19y agoI tried to pass the codeProc.call in the block but it wouldn't process the data until I passed the codeProc as a parameter. I don't really understand why to be honest.
- bct 19y agoI don't know why it wouldn't have. You should have been able to do: out = parseClosure(curr_page, cnt) { |doc,out| eval(codeStr) } ... def parseClosure(curr_page, cnt) ... yield(doc, out) or: def parseClosure(curr_page, cnt, &codeProc) There's not much opportunity to use lambda explicity in Ruby because using blocks instead is so easy.
- Readmore 19y agoYou are correct sir! I bow to your ruby-fu! Thanks.
- jdvolz 19y agoI feel the same way about Ruby and Javascript. I can't imagine what I was doing where I could have been using "eval" in C#. I was having to write my own interpreters inside the program and then pass around strings, which is crappy.