Using instance variables in Ruby

Text from About.com –

Source: http://ruby.about.com/od/variables/a/Instance-Variables.htm

“Now that you know what instance variables are, when do you use them? Instance variables should be used when they represent the state of the object. A student’s name and age, their grades, etc. They shouldn’t be used for temporary storage, that’s what local variables are for. However, they could possibly be used for temporary storage between method calls for multi-stage computations. However if you’re doing this, you may want to rethink your method composition and make these variables into method parameters instead.”

Apartamento novo para venda – Vila Andrade – São Paulo/SP – (11) 99500-4519

Apartamento com vista total para o verde.

Edifício com excelente infra-estrutura (2 piscinas,  coberta e descoberta, quadra poliesportiva, churrasqueira e forno para pizza, academia, brinquedoteca e salão de festas

Sacada com churrasqueira.

Piso de porcelanato colocado em todo o apartamento.

Apartamento novinho, nunca habitado.

Edifício Luz, Condomínio Fotografia luz_2 luz_4 luz_5 luz_6 luz_7 luz_8 luz_9 SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC SONY DSC3 quartos(suítes)
2 vagas de garagem
Cozinha com abertura para a sacada

Apartamento de 3 suítes – novo – sacada com churrasqueira e vista para o verde

Ruby tips – returning a value of a hash

2.0.0-p247 :001 > board = {1=>’alexandre’, 2=>’beto’, 3=>’carlos’, 4=>’daniel’, 5=>’ernesto’}

2.0.0-p247 :014 > board.select{|_,v| v == “alexandre” }

=> {1=>”alexandre”}

So, if I only use the ‘select’ method, irb returns a hash.

If I only need the value, I must use the  ‘values’ method.

Then…

2.0.0-p247 :001 > board = {1=>’alexandre’, 2=>’beto’, 3=>’carlos’, 4=>’daniel’, 5=>’ernesto’}

2.0.0-p247 :018 > board.select{|_,v| v == “alexandre”}.values

=> [“alexandre”]

Ruby – Hash iterators

Creating a hash on irb….

2.0.0-p247 :001 > board = {1=>’alexandre’, 2=>’beto’, 3=>’carlos’, 4=>’daniel’, 5=>’ernesto’}

=> {1=>”alexandre”, 2=>”beto”, 3=>”carlos”, 4=>”daniel”, 5=>”ernesto”}

Now, iterating over it:

2.0.0-p247 :008 > board.each { |key, value| puts “#{key} #{value}” }

1 alexandre

2 beto

3 carlos

4 daniel

5 ernesto

=> {1=>”alexandre”, 2=>”beto”, 3=>”carlos”, 4=>”daniel”, 5=>”ernesto”}

Another iteration…adding a surname: 

2.0.0-p247 :012 > board.each { |key, value| puts “#{value} surname” }

alexandre surname

beto surname

carlos surname

daniel surname

ernesto surname

=> {1=>”alexandre”, 2=>”beto”, 3=>”carlos”, 4=>”daniel”, 5=>”ernesto”}

Seguir

Obtenha todo post novo entregue na sua caixa de entrada.