small medium large xlarge

Posts by Steve Castaneda

Generic-user-small
21 Feb 2011, 03:33
Steve Castaneda (11 posts)
(from Agile Web Development with Rails 3.2 > Using join - why the argument?)

In: depot_c/test/unit/product_test.rb (early in the book - sorry reading on iPad)

I noticed that on the test “product is not valid without a unique title”, the assertion at the end “assert_equal” uses an argument on the join when comparing the error message.

For this particular test, we know what the error should be and there should only be one. Would it be safe to say that the argument that’s passed on the join is pointless (’; ‘)?

product.errors[:title].join(‘; ‘)

as opposed to just:

product.errors[:title].join

From what I understand, the argument is used to combine multiple values of an array (so if you put a dash the resulting string would list the values separated by a dash), but in this case we know that the array in the errors hash for :title will only have one value.

PS. This is my attempt to completely understand everything as I read. I really want to get it this time around!

PSS. Is this what people describe when they talk about “elegant” coding or is this just a simple refactor?

Generic-user-small
27 Feb 2011, 17:16
Steve Castaneda (11 posts)
(from Agile Web Development with Rails 3.2 > Starting from depot_o)

Learning to use git while developing is very crucial, in my opinion. I would highly recommend you work out the problems you have so that you can use it while coding.

I make tons of coding mistakes too, and love to try new things and git is an invaluable tool here for not messing up my work that I know is properly functioning.

Generic-user-small
02 Mar 2011, 15:55
Steve Castaneda (11 posts)
(from Agile Web Development with Rails 3.2 > Problem with Ajax (F2))

I was having this issue at one point, and putting script tags in the js file did the trick. You can also try removing the :format => :js, and moving that logic into the controller at the top:

respond_to :js, :html

Then in the particular method, in this case the “change” method:

def change
@variable = whatever

respond_with(@variable) do |format|
  format.html { redirect_to whereever_path }
  format.js
end

This should cause the file to render the JS file correctly, which should be named change.js.erb and in the same directory as the view you’re rendering.

Does that do the trick?

Generic-user-small
11 Mar 2012, 10:26
Steve Castaneda (11 posts)
(from Programming Ruby 1.9 (3rd edition) > Virtual Attributes - Why no instance variable used?)

Under Objects and Attributes > Virtual Attributes, where we look at:

tut_classes/stock_stats/book_in_stock_7.rb

I’m curious as to why, in the price_in_cents method, a local variable is used for price instead of an instance variable?

I thought I understood that local variables were encapsulated inside a method, but in this case, price is being called when not being passed into the method.

See: Integer(price*100 + 0.5)

Can anyone help me understand why a local variable was used for price, instead of an instance variable?

Generic-user-small
13 Mar 2012, 19:23
Steve Castaneda (11 posts)
(from Programming Ruby 1.9 (3rd edition) > Virtual Attributes - Why no instance variable used?)

Ah, I see now. So is it likely to confuse a local variable with a method then (since the format is the same), or would it just be best practice to not use local variables at all? For example, say I created a method which took a parameter called price, forgetting that I already have an attr_accessor defined. Possible issue or would the local variable take precedence?

Thank you for the guidance, by the way.

11 posts