Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > idiom I've not seen before

Reply
Thread Tools

idiom I've not seen before

 
 
Deepak Gole
Guest
Posts: n/a
 
      02-08-2008
[Note: parts of this message were removed to make it a legal post.]

Hello Rob

We can send arguments, Following is the way, But it throws an error.

def to_proc
Proc.new { |*args| args.shift.__send__(self, *args) }
end

arr.collect(&[:concat,"Rocking"])

* *sample.rb:41:in `__send__': [:concat, "dee"] is not a symbol
(TypeError)
from sample.rb:41:in `to_proc'
from sample.rb:51:in `collect'
from sample.rb:51

But if we change the implementation of to_proc then it works.

def to_proc
lambda {|target| target.send *self}
end

arr.collect(&[:concat,"Rocking"])

o/p => ["rubyRocking", "railsRocking", "rjaxRocking"]

I don't know how.


On Feb 6, 2008 8:04 PM, Rob Biedenharn <(E-Mail Removed)> wrote:

> On Feb 6, 2008, at 10:55 PM, Rob Saul wrote:
> > I came across this :
> > elements.sort_by(&osition).each
> > in something I've taken over. I thought sort_by
> > took a block. Obviously I'm missing something.
> > Can anyone shed some light on this trick?
> >
> > ~Rob

>
>
> You've found Symbol#to_proc
>
> That line is equivalent to:
>
> elements.sort_by {|e| e.send(osition) }.each
>
> The "something" is a Rails project, isn't it?
>
> == vendor/rails/activesupport/lib/active_support/core_ext/symbol.rb ==
> class Symbol
> # Turns the symbol into a simple proc, which is especially useful
> for enumerations. Examples:
> #
> # # The same as people.collect { |p| p.name }
> # people.collect(&:name)
> #
> # # The same as people.select { |p| p.manager? }.collect { |p|
> p.salary }
> # people.select(&:manager?).collect(&:salary)
> def to_proc
> Proc.new { |*args| args.shift.__send__(self, *args) }
> end
> end
>
>
> I don't know how you can send additional arguments however.
>
> -Rob
>
> Rob Biedenharn http://agileconsultingllc.com
> http://www.velocityreviews.com/forums/(E-Mail Removed)
>
>
>
>


 
Reply With Quote
 
 
 
 
ThoML
Guest
Posts: n/a
 
      02-08-2008
> Io (like Lisp, I gather) allows lazy evaluation of method arguments.

Are you referring to Lisp's macro facility? AFAIK macros are usually
expanded at load/compile time. Since everything is a s-expression in
lisp, the macro gets its arguments as list and can process them. The
macro call is then replaced with the result. Or something along this
line.

It seems Io provides a similar facility to treat code as data, which
is cool. Unfortunately I'm not able to compile it. IIRC Forth was able
to do something similar too BTW (just a side-note).

Since ruby doesn't have this feature (unless you use parsetree and
ruby2ruby maybe), the ruby equivalent are strings, I'd say. One could
thus write something like:

class Array
def select_by(snippet)
rv = []
self.each do |e|
rv << e if e.instance_eval '%s %s' % [e.inspect,
snippet]
end
rv
end
end

a = [1,2,3,4,5]
a.select_by '> 3'

Which is about the same as your io example. It would be nice of course
to be able to have real macros that are expanded at load time so that
one could write something like the following which is incorrect ruby
syntax and would have to be rewritten before the parser tries to make
sense out of it:

module Macros
def select_by(code)
%{select {|e| e #{code}}}
end
end

a = [1,2,3,4,5]
a.select_by(> 3)

This would require that macro names are unique though. But it's just a
delirious day-dreaming anyway.

Regards,
Thomas.

 
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
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com Java 0 03-06-2008 08:11 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C++ 0 03-05-2008 08:41 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C Programming 0 03-05-2008 03:26 AM
had not seen this before Computer Support 8 07-28-2004 01:19 AM
Computer problems not seen before David Computer Support 7 06-26-2004 01:43 AM



Advertisments