instance-variables

Ruby attr_reader allows one to modify string variable if using <<

别等时光非礼了梦想. 提交于 2019-12-03 15:04:11
Ran into some weird behaviour and wondering if anyone else can confirm what I am seeing. Suppose you create a class with a member variable, and allow it to be read with attr_reader. class TestClass attr_reader :val def initialize(value) @val = value end end Now when I do the following, it seems to modify the value of @val, even though I have only granted it read privileges. test = TestClass.new('hello') puts test.val test.val << ' world' puts test.val This returns hello hello world This is just the result from some testing I did in irb so not sure if this is always the case You are not really

Why use instance variables to “connect” controllers with views?

霸气de小男生 提交于 2019-12-03 12:53:32
This is a conceptual question and I haven't been able to find the answer in SO, so here I go: Why instance variables are used to connect controllers and views? Don't we have two different objects of two different classes (Controller vs Views). So, when the view is rendered we are in a different context, but we are using instance variables of another object? Isn't this breaking encapsulation in somehow? How does Rails manage to do that matching from one object to another? Does it clone all the instances variables of the controller to the view? In a sense, you could say that it is breaking

Why is it possible to override instance variables in PHP but not in Java?

淺唱寂寞╮ 提交于 2019-12-03 07:44:35
Consider the code below: <?php class Base { protected $name = "Base"; public function getName() { return $this->name; } } class Foo extends Base { protected $name = "Foo"; } $f = new Foo(); echo $f->getName(); // output: Foo $b = new Base(); echo $b->getName(); // output: Base Since in other languages such as Java won't allow you to override the instance variable, but it is possible in PHP. Is it because since PHP is weak type language so it is possible? No, it has nothing to do with weak typing . I guess this was simply a design decision that the PHP developers took. It may be because it is

Declaring instance variables iterating over a hash!

流过昼夜 提交于 2019-12-03 07:22:39
问题 i want to do the following: I want to declare the instance variables of a class iterating over a dictionary. Let's assume that i have this hash hash = {"key1" => "value1","key2" => "value2","key3" => "value3"} and i want to have each key as instance variable of a class. I want to know if i could declare the variables iterating over that hash. Something like this: class MyClass def initialize() hash = {"key1" => "value1","key2" => "value2","key3" => "value3"} hash.each do |k,v| @k = v end end

How to deal with Pylint's “too-many-instance-attributes” message?

做~自己de王妃 提交于 2019-12-03 04:30:54
问题 I have just tried to lint some code with Pylint, and the last remaining error is R0902: too-many-instance-attributes (8/7) I understand the rationale behind limiting the number of instance attributes, but seven seems a bit low. I also realise that the linter should not have the last word. However, I would like to know what I should be doing instead of: def __init__(self, output_file=None, output_dir=None): """ Set the frobnicator up, along with default geometries """ self.margin = 30 self.pos

Rails — self vs. @

老子叫甜甜 提交于 2019-12-03 03:18:18
问题 I am following Michael Hartl's RoR tutorial, and it is covering the basics of password encryption. This is the User model as it currently stands: class User < ActiveRecord::Base attr_accessor :password attr_accessible :name, :email,: password, :password_confirmation email_regex = /^[A-Za-z0-9._+-]+@[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+[A-Za-z]$/ #tests for valid email addresses. validates :name, :presence => true, :length => {:maximum => 50} validates :email, :presence => true, :format => {:with =

java sql connections via class

风流意气都作罢 提交于 2019-12-02 23:51:32
问题 Hy i have the following code : import java.sql.*; import java.net.*; public class binsz { public void dbConnect(String db_connect_string, String username, String password) { try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); Connection conn = DriverManager.getConnection(db_connect_string, username, password); // System.out.println("connected"); } catch (Exception e) { e.printStackTrace(); } } public void dbQuery(String query) { try { Statement stmt = conn.createStatement(); ResultSet rs

Use NSString variable created in viewDidLoad inside another method

拟墨画扇 提交于 2019-12-02 22:09:17
问题 In my viewDidLoad method, I set the following variables: // Get requested URL and set to variable currentURL NSString *currentURL = self.URL.absoluteString; //NSString *currentURL = mainWebView.request.URL.absoluteString; NSLog(@"Current url:%@", currentURL); //Get PDF file name NSArray *urlArray = [currentURL componentsSeparatedByString:@"/"]; NSString *fullDocumentName = [urlArray lastObject]; NSLog(@"Full doc name:%@", fullDocumentName); //Get PDF file name without ".pdf" NSArray *docName

Iterate and Set Ruby Object Instance Variables

喜你入骨 提交于 2019-12-02 19:06:29
问题 I'm looking for a way to iterate through a Ruby object's instance variables, and set them individually (from a provided hash) using a generic setter. I am assuming I can iterate through them directly in a method and simply set each individually. Here is my object instance, u: u = #<Waffle:0x00007fc6b1145530 @id=nil, @alpha="", @bravo="", @charlie="", @delta=nil, @echo=nil, @status=new> I would like to populate it with a given hash, h: h = { "id"=>"141", "alpha"=>"Muccahiya"" "bravo"=>"$2a$10

How to deal with Pylint's “too-many-instance-attributes” message?

一世执手 提交于 2019-12-02 16:58:58
I have just tried to lint some code with Pylint, and the last remaining error is R0902: too-many-instance-attributes (8/7) I understand the rationale behind limiting the number of instance attributes, but seven seems a bit low. I also realise that the linter should not have the last word. However, I would like to know what I should be doing instead of: def __init__(self, output_file=None, output_dir=None): """ Set the frobnicator up, along with default geometries """ self.margin = 30 self.pos = [0, 0] self.sep = [5, 5] self.cell = [20, 20] self.frobbr = library.Frobbr() page = self.frobbr.get