instance-variables

Class methods (ruby)

时光毁灭记忆、已成空白 提交于 2019-12-08 06:45:29
问题 Newbie here, having a hard time understanding Class methods and why I cannot get an attribute to show up correctly in the instance. class Animal attr_accessor :noise, :color, :legs, :arms def self.create_with_attributes(noise, color) animal = self.new(noise) @noise = noise @color = color return animal end def initialize(noise, legs=4, arms=0) @noise = noise @legs = legs @arms = arms puts "----A new animal has been instantiated.----" end end animal1 = Animal.new("Moo!", 4, 0) puts animal1

Instance variable in Stripes

北战南征 提交于 2019-12-08 05:26:26
问题 I'm trying to find a way to create an instance variable within the Stripes application context. Something that i would do in the init() method of a Servlet while using hand-coded servlets. The problem is that since an instance of the ActionBean is created each time the application is accessed, the variable in the actionBean is created multiple time. I have tried to get some reasonable place withing Stripes trying to call the ServletContext via ActionBeanContext.getServletContext() , but from

Why can't I directly access instance variables in Active Record callback?

落爺英雄遲暮 提交于 2019-12-08 04:26:11
问题 In Active Record callbacks, I always see examples using self.variable . Sometimes, the self. prefix isn't needed, so they use just variable (see this related question). From what I've read, these two methods of referencing the class instance variable are using the accessor function, whereas @variable would directly access the variable. In my code, I tried to use @variable and it didn't work - it was as if @variable was not defined yet... can you not access instance variables directly in an

Initialising instance variables as null, “” or 0

无人久伴 提交于 2019-12-08 02:27:32
问题 When initialising variables with default values: What is the difference between: private static String thing = null; and private static String thing = ""; I'm not understanding which is better and why nor what is the best way to deal with other data types. private static int number = 0; private static double number = 0; private static char thing = 0; Sorry I struggle learning new languages. 回答1: Except for initializing String to an empty string private static String thing = ""; the other

New instance of a UIViewController within another UIViewController: Why can't I set an instance variable?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 20:11:19
问题 So I have a UIViewController subclass called MyTabBarViewController that has a UIScrollView. Inside of MyTabBarViewController I'm creating an instance of another UIViewController subclass called PhotoViewController. (Note: I'm doing this so I can set up the IBOutlets using IB) I'm trying to set the label of each PhotoViewController instance from my TabBarViewController. And I init with nib for each PhotoViewController so I was under the impression that each PhotoViewController instance would

How do you use an instance variable with mailer in Ruby on Rails?

狂风中的少年 提交于 2019-12-07 13:59:20
问题 I created an instance variable (@user) in the model of a mailer and want to access it in the view? But it's giving me an an error (@user = nil). What's the best way to pass a variable to view (the email body)? Thanks, Chirag 回答1: If you want to access an instance variable in your mailer template then in your mailer model add @body[:user] = user_object The above would create an instance variable @user that can be accessed in your view. You should be able to access user object in your mailer

What is a good way to memoize data across many instances of a class in Ruby?

佐手、 提交于 2019-12-07 12:21:14
问题 Consider: many instances of an object that generates data. It would be great to only generate that data once per run. class HighOfNPeriods < Indicator def generate_data @indicator_data = DataStream.new (0..@source_data.data.count - 1).each do |i| if i < @params[:n_days] ... @indicator_data.add one_data end end There are different instances of HighOfNPeriods with different params and different @source_data . Here is how the indicator is used: class Strategy attr_accessor :indicators def

Passing an instance variable into a form - rails

偶尔善良 提交于 2019-12-07 12:16:15
问题 This is likely an error due to my minimal understanding of Rails and how to use variables across models, so if there is more code needed to answer it or if my terminology is incorrect, let me know and I will gladly update the question. I have a feed of posts that I want a user to be able to "like." While the following code allows likes to work on an individual post's page - site.com:3000/posts/*post.id* - with the form data being passed of like[liked_post_id]:*post.id* , when I try to submit

Instance variable not set with redirect

偶尔善良 提交于 2019-12-07 10:55:51
问题 What would cause my instance variable @product to not be set/passed for the redirect. Product is an ActiveModel object, not ActiveRecord. to be more specific, the @product variable is not appearing in the redirect_to(new_services_path) or redirect_to(home_path) pages. As the @product variable need to populate a form in my footer that is on every page. Application_controller: class ApplicationController < ActionController::Base before_filter :set_product private def set_product @product ||=

Why does this ivar need @protected if @protected is the default?

扶醉桌前 提交于 2019-12-07 10:45:20
问题 @interface AClass : SomeType { @protected NSMutableArray* amINotAlreadyProtected; //? } Why does this code need @protected if @protected is the default? This code was written by a very experienced programmer, but I would omit the specifier myself. 回答1: There is no need for the keyword @protected as it is the default behavior. However, some programmers tend to use it anyways incase a less experienced programmer comes along at a later date and doesn't know this. It can also be mentioned that it