Forum: Ruby how can we call a method without using . operator

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ruby r. (Guest)
on 2009-01-19 17:16
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?
Brian C. (Guest)
on 2009-01-19 17:26
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?
James C. (Guest)
on 2009-01-19 17:28
(Received via mailing list)
2009/1/19 ruby rails <removed_email_address@domain.invalid>

> 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 C. (Guest)
on 2009-01-19 17:38
(Received via mailing list)
> foo_object :bar_method
>


Correction: final line should have been
foo :bar_method
ruby r. (Guest)
on 2009-01-19 17:47
James C. wrote:
> 2009/1/19 ruby rails <removed_email_address@domain.invalid>
>
>
> 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...
Robert K. (Guest)
on 2009-01-19 20:11
(Received via mailing list)
On 19.01.2009 16:45, ruby rails wrote:
> James C. wrote:
>> 2009/1/19 ruby rails <removed_email_address@domain.invalid>
>>
>>
>> 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
Brian C. (Guest)
on 2009-01-19 22:47
Robert K. wrote:
> No meta programming needed
>
> irb(main):001:0> "foo".instance_eval { puts length }

Foul ... you used a dot!
The H. (Guest)
on 2009-01-19 23:30
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.
Robert K. (Guest)
on 2009-01-19 23:51
(Received via mailing list)
On 19.01.2009 21:46, Brian C. wrote:
> Robert K. 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
Simon C. (Guest)
on 2009-01-20 17:28
(Received via mailing list)
One more way uses colons:

  a = [1,2]
  a::length      # => 2
  a::push(3)     # => [1,2,3]
  a::length      # => 3
This topic is locked and can not be replied to.