private-methods

Should I test private methods using RSpec?

耗尽温柔 提交于 2019-12-20 09:30:02
问题 Is it good practice to write tests for private methods? Consider the following simple example: class Group has_many :members private def release_members members.each { |member| member.update_attributes group_id: nil } end end Would it be good practice to write a test for the release_members method in RSpec? I believe you'd have to write the test calling the method with send ie. group.send(:release_members) which is sometimes frowned upon. 回答1: You can find an in-depth discussion of that very

Private Methods Over Public Methods

心不动则不痛 提交于 2019-12-17 23:44:32
问题 I was examining the StringTokenizer.java class and there were a few questions that came to mind. I noticed that the public methods which are to be used by other classes invoked some private method which did all of the work. Now, I know that one of the principles of OOD is to make as much as you can private and hide all of the implementation details. I'm not sure I completely understand the logic behind this though. I understand that it's important to make fields private to prevent invalid

How do I use reflection to invoke a private method?

泄露秘密 提交于 2019-12-16 20:13:24
问题 There are a group of private methods in my class, and I need to call one dynamically based on an input value. Both the invoking code and the target methods are in the same instance. The code looks like this: MethodInfo dynMethod = this.GetType().GetMethod("Draw_" + itemType); dynMethod.Invoke(this, new object[] { methodParams }); In this case, GetMethod() will not return private methods. What BindingFlags do I need to supply to GetMethod() so that it can locate private methods? 回答1: Simply

How to write a “truly” private method in C#?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 09:41:17
问题 In fact, private methods are implemented in C# that can still be searched with Reflection . What I am going to do is to write public string Encrypt(string data) and private string Decrypt(string cipher) methods to perform encryption and decryption. Unfortunately, if someone knows .NET framework, he can use Reflection to find Decrypt methods and it decrypt everything that is encrypted. It seems that is not that secure. So I want to make Decrypt method to truly private method. But how to do

Hidden instance variables in before_filter method

≡放荡痞女 提交于 2019-12-11 10:12:02
问题 Is it a good practice to hide instance variable initialization in private methods? For example, I have a user controller with some actions: class UsersController < ApplicationController before_filter :get_user, only: [:show, :edit, :update, :destroy] before_filter :set_user, only: [:new, :create] def index @users = User.all end def show end def new end def edit end def create if @user.save redirect_to @user, notice: 'User was successfully created.' else render action: 'new' end end def update

assign value of readonly variable in private method called only by constructors

痞子三分冷 提交于 2019-12-10 12:40:05
问题 C# compiler gave me the following error CS0191: A readonly field cannot be assigned to (except in a constructor or a variable initializer) Do I have to move the code (in my private function) into the constructor? That sounds awkward. Note that the private method was intended only to be called by the constructor. I expect that there is some sort of attribute that I can use to mark the method corresponding. 回答1: Despite what the other posts are saying, there is actually a (somewhat unusual) way

Can one access private member functions through casting to layout-compatible types?

依然范特西╮ 提交于 2019-12-09 17:58:49
问题 From the discussion of this question How is access for private variables implemented in C++ under the hood? I posed a variation: instead of accessing a private data member, can one call private member functions through casting and relying on layout-compatibility? Some code (inspired by Herb Sutter's column Uses and Abuses of Access Rights ) #include <iostream> class X { public: X() : private_(1) { /*...*/ } private: int Value() { return private_; } int private_; }; // Nasty attempt to

How to write a “truly” private method in C#?

て烟熏妆下的殇ゞ 提交于 2019-12-05 14:06:42
In fact, private methods are implemented in C# that can still be searched with Reflection . What I am going to do is to write public string Encrypt(string data) and private string Decrypt(string cipher) methods to perform encryption and decryption. Unfortunately, if someone knows .NET framework, he can use Reflection to find Decrypt methods and it decrypt everything that is encrypted. It seems that is not that secure. So I want to make Decrypt method to truly private method. But how to do that? Updated 09 Jan 2012 10:52PM Sydney Time bdares provides the technical explanation of this question

Can one access private member functions through casting to layout-compatible types?

落爺英雄遲暮 提交于 2019-12-04 06:21:33
From the discussion of this question How is access for private variables implemented in C++ under the hood? I posed a variation: instead of accessing a private data member, can one call private member functions through casting and relying on layout-compatibility? Some code (inspired by Herb Sutter's column Uses and Abuses of Access Rights ) #include <iostream> class X { public: X() : private_(1) { /*...*/ } private: int Value() { return private_; } int private_; }; // Nasty attempt to simulate the object layout // (cross your fingers and toes). // class BaitAndSwitch // hopefully has the same

How do I define private or internal methods in object oriented Perl?

这一生的挚爱 提交于 2019-12-04 03:14:57
I'm using Damian Conway's "inside-out" objects as described is his wonderful book Perl Best Practices to construct an object-oriented interface to a security system at my client. I'm coming across the need to use internal helper methods within my module that I would normally designate as "_some_method". However, this seems to break encapsulation since they can be called directly via the package name. Is there any way of making these methods truly private? As an example, use SOD::MyOOInterface; my $instance1 = SOD::MyOOInterface->new(); $instance1->_some_method; #this produces an error: SOD: