[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how can we call a method without using . operator

Venkat Bagam

1/19/2009 3:15:00 PM

Hi all,
This might sound a bit wierd but I would like to know if
there are any other ways of calling a method without using . operator or
calling send().

Is it possible using blocks or something else?
--
Posted via http://www.ruby-....

9 Answers

Brian Candler

1/19/2009 3:25:00 PM

0

ruby rails wrote:
> This might sound a bit wierd but I would like to know if
> there are any other ways of calling a method without using . operator or
> calling send().

(or __send__ presumably). How about this one?

def foo
puts "hello"
end

m = method(:foo)
m[]

What are you trying to achieve anyway?
--
Posted via http://www.ruby-....

James Coglan

1/19/2009 3:27:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

2009/1/19 ruby rails <bagam_venkat@hotmail.com>

> Hi all,
> This might sound a bit wierd but I would like to know if
> there are any other ways of calling a method without using . operator or
> calling send().



You could write a DSL that would make it look like you weren't doing dot or
send() but at some level you're going have to use one of those, or maybe
instance_eval. e.g.:

objects = {
:foo => foo_object
}

def method_missing(name, method)
objects[name.to_sym].__send__(method)
end

# calls foo_object.bar_method
foo_object :bar_method

It might be helpful if we knew the problem you're trying to solve -- can you
give us any more info?

James Coglan

1/19/2009 3:34:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

>
> objects = {
> :foo => foo_object
> }
>
> def method_missing(name, method)
> objects[name.to_sym].__send__(method)
> end
>
> # calls foo_object.bar_method
> foo_object :bar_method
>


Correction: final line should have been
foo :bar_method

Venkat Bagam

1/19/2009 3:46:00 PM

0

James Coglan wrote:
> 2009/1/19 ruby rails <bagam_venkat@hotmail.com>
>
>
> It might be helpful if we knew the problem you're trying to solve -- can
> you
> give us any more info?
I was asked this question by one of y interviewer...May be he is testing
my meta-programming skills...
--
Posted via http://www.ruby-....

Robert Klemme

1/19/2009 6:09:00 PM

0

On 19.01.2009 16:45, ruby rails wrote:
> James Coglan wrote:
>> 2009/1/19 ruby rails <bagam_venkat@hotmail.com>
>>
>>
>> It might be helpful if we knew the problem you're trying to solve -- can
>> you
>> give us any more info?
> I was asked this question by one of y interviewer...May be he is testing
> my meta-programming skills...

No meta programming needed

irb(main):001:0> "foo".instance_eval { puts length }
3
=> nil

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

Brian Candler

1/19/2009 8:46:00 PM

0

Robert Klemme wrote:
> No meta programming needed
>
> irb(main):001:0> "foo".instance_eval { puts length }

Foul ... you used a dot!
--
Posted via http://www.ruby-....

The Higgs bozo

1/19/2009 9:30:00 PM

0

ruby rails wrote:
> Hi all,
> This might sound a bit wierd but I would like to know if
> there are any other ways of calling a method without using . operator or
> calling send().
>
> Is it possible using blocks or something else?

Look Ma, no dots!

class Foo
def hello
puts "hello!"
end
end

eval "Foo\056new\056hello"
#=> hello!

I will send my contact info via email so that I may receive my prize.
--
Posted via http://www.ruby-....

Robert Klemme

1/19/2009 9:47:00 PM

0

On 19.01.2009 21:46, Brian Candler wrote:
> Robert Klemme wrote:
>> No meta programming needed
>>
>> irb(main):001:0> "foo".instance_eval { puts length }
>
> Foul ... you used a dot!

Uh, oh! I thought nobody would notice. Cough cough.

;-)

robert

--
remember.guy do |as, often| as.you_can - without end

bahuvrihi

1/20/2009 3:26:00 PM

0

One more way uses colons:

a = [1,2]
a::length # => 2
a::push(3) # => [1,2,3]
a::length # => 3