Forum: Ruby dot 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.
478deb011156847f3e4c9dbcc3899cfd?d=identicon&s=25 Sai Charan (charan90)
on 2017-04-04 13:16
I am trying to use a dynamic method after the dot operator, its not
identifying. Please help me in resolving this is an urgent issue.

@opt1 has already a method which is same as the def_name assigned value


def_name = field_details[0].downcase.tr('-', '')
@opt1.def_name = details['fieldvalue']


Please help......

Thanks,
Sai
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-05 10:09
I'm not sure whether I understand your question correctly. Let's do an
example: Assuming that def_name evaluates to the String 'foo', is it
correct that @opt1 understands a

   @opt1.foo = ....

? In this case, you can do it like this:

  @opt1.send_public((def_name+'=').to_sym, details['fieldvalue'])

Ronald
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2017-04-06 06:57
I have no real idea what you are trying to do here but:

> def_name = field_details[0].downcase.tr('-', '')
> @opt1.def_name = details['fieldvalue']

Do you try to dynamically call a method? In this case
you could try to use .send().

I do not understand why you show only two lines of
code. Errors could be elsewhere in your code.

Always try to showcase the smallest part of where
the problem is AND the specific error too.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-06 10:54
@Robert: I admit that the OPs question is a bit unclear, but I assume
that he wants to invoke a setter method where he has the name of the
corresponding getter method as a string.

I would recommend against using Object#send, as you suggested, but use
Object#public_send instead. It's safer.

Ooops, I see that in my own response, I misspelled the method name. Will
fix it.
0fa73332c8e4a3b06ea439fd3f034322?d=identicon&s=25 Ronald Fischer (rovf)
on 2017-04-06 10:55
Just a notice (because I can't edit my earlier posting anymore):

The method you need to invoke is 'public_send', not 'send_public', as I
wrote. Sorry for the confusing typo.

Ronald
C20ecde873c2189cf00aef2b178c4ee0?d=identicon&s=25 Rahul Parashar (rubytrainee)
on 2017-04-07 08:40
Sai Charan wrote in post #1185750:
> I am trying to use a dynamic method after the dot operator, its not
> identifying. Please help me in resolving this is an urgent issue.
>
> @opt1 has already a method which is same as the def_name assigned value
>
>
> def_name = field_details[0].downcase.tr('-', '')
> @opt1.def_name = details['fieldvalue']
>
>
> Please help......
>
> Thanks,
> Sai

I'm not trying to be sarcastic, just passing along a very useful tool.

I usually just put Ruby,
Class, Datatype" into Google and the appropriate rubydoc pops up

This is very handy.

https://www.sagipl.com/ruby-on-rails-development/
478deb011156847f3e4c9dbcc3899cfd?d=identicon&s=25 Sai Charan (charan90)
on 2017-04-07 16:55
Thanks a lot guys figured out a way

screen.method(method_name).call(field_value)

This has worked
This topic is locked and can not be replied to.