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.
3984bc1791c4ad196a1ae2ab3d5fb06d?d=identicon&s=25 ruby rails (venkat1246)
on 2009-01-19 16: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?
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-19 16: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?
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-01-19 16:28
(Received via mailing list)
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?
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2009-01-19 16:38
(Received via mailing list)
> foo_object :bar_method
>


Correction: final line should have been
foo :bar_method
3984bc1791c4ad196a1ae2ab3d5fb06d?d=identicon&s=25 ruby rails (venkat1246)
on 2009-01-19 16:47
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...
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-19 19:11
(Received via mailing list)
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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-19 21:47
Robert Klemme wrote:
> No meta programming needed
>
> irb(main):001:0> "foo".instance_eval { puts length }

Foul ... you used a dot!
Beb77c4602c3cac7a12149431366ed11?d=identicon&s=25 The Higgs bozo (higgsbozo)
on 2009-01-19 22: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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-19 22:51
(Received via mailing list)
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
2e2d8c8ff86e18bf49f777f39028823e?d=identicon&s=25 Simon Chiang (bahuvrihi)
on 2009-01-20 16: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.