Dev box
I'm a 32-year-old Ruby on Rails web developer. With the help of friends and co-workers, I've been teaching myself Rails. I worked as a Rails developer for Crowd911 in Colorado. I live in Berlin.
In my developer blog I publish a stochastic array of thoughts and ideas, lessons, mistakes, questions and attempts as answers about Rails and Ruby. I am also posting Anki cards that I used to help myself study. In my view most knowledge is about practice. If you have thoughts, questions, ideas, corrections, arguments, feel free to email me at henryvw@gmail.com.

attr_accessor, attr_reader and attr_writer

Scannen 2

attr_accessor  :name

attr_reader  :name

attr_writer  :name

What do all of these do, and what do they have to do with instance variables?

If you want to create an attribute/instance variable for objects of a class — in this example, simply a name — you can either write methods to submit and display that variable, or you can use attr_reader and attr_writer, or attr_accessor, which combines both. If you only used attr_reader, you could create a read-only attribute.  If you only used attr_writer, you could create a write-only attribute.

Here’s what the methods look like, if you were to write them out yourself.  These are the methods that are hidden behind attr_accessor:

def set_name (aName)

@name = aName

end

 

def get_name

return @name 

end

Read More

Defining PROCS and LAMBDAS, and the huge exception to rule that everything in Ruby is an object: Blocks

Blocks procs and lambdas

Blocks are not objects.  That means blocks can’t be saved to variables, among other powers that they don’t have.  Blocks aren’t saved anywhere.  They appear and disappear.

A Proc or a Lambda is a block that is an object and is saved. So using Procs helps to DRY out your code.  You can always re-use a Proc!  Like a method, a Proc is like a variable that contains actions and verbs, rather than data.

To define a Proc, you usevariable = Proc.new

To define a Lambda, you use  variable = lambda {  }

To call a Proc or a Lambda, you convert it into a block using &.  

Example:

 integers = floats.collect(&round_down)

You can also call it using the .call method.  

Note that you cannot call Blocks using .call, only Procs and Lambdas

One important note: although you cannot save a Block to a variable, you cansave the output value returned by a block.

What is the difference between Procs and Lambdas?

#1 Lambdas pass the control back to the calling method when it returns. So if you say „return this“ in the Lambda, and then subsequently „return that“ in the Method, the Method will be the item that returns. 

The code above will 

puts „Iron Man will win!“ for the Lambda (because it returns control the method)

and 

puts „Batman will win“ for the Proc (because the Proc does NOT return control to the Method)

#2 It cares about the number of arguments it gets

How to remember the differences between Blocks, Procs and Lambdas?

Basically, it goes in that order of complexity - from Block, to Proc to Lambda. Blocks, Procs and Lamdas!  A Proc is just going to hit that method, and not think to let the method check anything else.  A Lambda is more complex, and enables the Method to come back and check it. 

Read More

Refresh Paperclip rake commands

rake paperclip:refresh:missing_styles

rake paperclip:refresh CLASS=Post

Read More

Check where you're deploying to Git and Heroku

Git remote  v

To Check which place you are deploying when you push on Git and on Heroku:

     git remote -v

 

It will show a result like the one in the screenshot.

 

Read More

Guess What - In Ruby, We Don’t Need a Return Statement!

Self in ruby

Guess What - In Ruby, We Don’t Need a Return Statement!

def add (a,b)

return a + b 

end

 

produces the same output as

 

def add(a,b)

a + b

end

Read More






Footerfull