Anyone know how to get the sender class/module in ruby?
caller[0] is helpful to derive the filename and linenumber sending.
But knowing the class would be helpful. Can't find it any searches?
This would be impossible. You shouldn't be specialising your behaviour in a method based on the calling class anyway.
Think about it this way - the caller could be an anonymous function (proc) created in one class, then given to another one and invoked from a third place. You wouldn't get anything useful.
Instead, I'd look at what you're trying to achieve here, and think of another way to get there! :)
Check out this gem: https://github.com/asher-/sender
Related
Sorry this is probably a question that has been asked many times but as it regards what is a very common word in English it is pretty much impossible to google or search for it.
I have seen a few examples of Ruby code which looks like this:
EnquiryNotification.with(post: #post)
I'm trying to understand what the ".with" part does. Can anyone shed some light on this please?
with is a class method defined on the EnquiryNotification class (or one of its ancestors). The method is neither a keyword in the Ruby language nor is it a common method on classes shipped with Ruby itself or its standard library. As such, to find what this method does, you would likely have to consult the documentation of your chosen framework or application.
An example of a with method defined in a framework is Sequel::Dataset#with to add a CTE to the current query. The method is also available as a class method in Sequel model classes.
It could also be part of ActionMailer as mentioned by Stefan in a comment above.
In any case though, make sure to consult the documentation of your chosen framework or library for details.
I'm writing a function that iterates over the methods on a given struct and binds the methods to handlers. I would like to skip over internal methods if possible. I'm not sure if this is possible to do so explicitly - I reviewed the documentation for the reflect package and I didn't see a means to detect if a given Value is an internal method. I know I can get the method's name, and then check if it starts with a lowercase character but I'm not sure if there's a kosher way to accomplish this. It's also possible that the internal / public boundary really only exists at compile time anyways, so there really isn't even a way of knowing this beyond the method's name. In either case I'd like to know for sure. Thanks!
The reflect package will not give you unexported methods via Type.Method. Pretty much, if you can see it via reflect, it is already exported.
See https://play.golang.org/p/61qQYO38P0
Am I missing out something?
class Circus
private
def start
puts 'And now for something completely different..'
end
end
And for my disillusion:
c=Circus.new
c.start #NoMethodError: private method `start' called
c.method(:start).call #no problem at all
c.send :start #neither this fails
Can anybody give me a reason why private methods exist in Ruby?
Put it this way. In Ruby you could just as easily monkey-patch a public method into a class which invoked the private method, so... Why would private prevent your behaviour?
You seem to think that private methods are somehow a security thing, that they have to be inaccessible outside the class. Encapsulation isn't about security, is a tool for producing clean, maintainable code. It's up to you to respect the interface exposed by a class. If you really want to find a way to invoke a private method, you will. Ruby cannot enforce this completely, nor should it have to; it would be counter-productive and a waste of time.
I mean, can anybody give me at least one reason why 'private' even exists in Ruby?
For the exact same reason private exists in any other OOP-based language: Encapsulation. Really, you think that just because you can find a convoluted way to circumvent private, it should just be thrown out?
It declares the intent of the method. If it's private you're not supposed to call it and you should be aware that doing so may cause unpredictable or undesirable behaviour.
For what it's worth, this behaviour is not limited to Ruby - it is possible to access private members and variables in Java as well, via the Reflection API.
Private methods are supposed to be called only from inside the same instance, without explicit receiver. They will fail whenever explicit receiver is supplied, including self.some_private_method called from the same instance. The ability to bypass this via send is a nonstandard coding practice, that should be used mainly for maintenance.
Once you start using reflection, all bets are off. That's not unique to Ruby, by the way, almost every language that has reflection has this problem.
You can mitigate this by using coding standards and code reviews, e.g. always use public_send etc. But if you don't like this, you will need to use a language with a properly stratified reflection system such as Newspeak.
I'm trying to call a function, and VS gives me an error (red underline), and i have the option to "generate method stub". What is this?
The generate method stub will generate you a method which looks exactly like you've written it, with the same parameters. Probably are getting this error because you've misspelled the method or because it is in a different namespace.
It means that you're trying to call the function incorrectly; check to make sure you've spelled the method name correctly, and that you're passing it the proper number and types of arguments.
It means you typed a wrong signature, so VS assumes this method doesn't exist. By using the shortcut VS can help you create the method as a stub (i.e. the signature, then you have to fill out the implementation).
ahh I had
method(button.Tag);
and a declaration of
void method(int tag)
so i fixed it with
method(int.Parse(button.Tag.toString()));
i tried that before, but I forgot to put "toString", since I thought it was already an int... stupid little mistake. thx guys
How to extract a name of the class from its object?
For example I have a #list object which I know is surely an instance of List class. But how do I extract that directly in code?
This kind of information is rather basic Ruby programming. The answer is:
object.class
Extra tip for the next time: try finding this information yourself in the core library documentation. You know you have some kind of object, just start reading the documentation and you will find some method that suites your needs. Information about the methods you can perform on an object can be found here.
If you want to test for an instance of a specific class, I'd go with something like:
#list.is_a?(List)
Like Edwin said, object.class will give you the corresponding Class object. If you just want the name of the class, use object.class.name.