Methods

Every time you see

def some_one
...
end

‘some_one’ is a method. Methods let you store a block a code and execute it when you call the method name. You can also use  a method to pass arguments. If I said some_one(name) that would be an argument for the method. I could then say:

def some_one(name)
puts "Hello #{name}"
end

Which would allow me to do this.

some_one("Kyle")
=> Hello Kyle

I can also use a default for the method argument.

def some_one(name = "there")
puts "Hello @{name}"
end

some_one()
Hello there

Those are really simple examples but there is no need to complicate things. A method stores some functionality that you can call later, it becomes more useful when you pass it arguments. In the Ruby documentation they give an example of a greeter method where they create a say_hi and say_bye method in that instance you could call a method depending on whether the user is coming or leaving the site. It’s common to group similar methods into a class, we don’t cover that here but there is a method called ‘initialize’ called inside of classes that is very important. You will see the initialize method a lot so it’s important you know what is happening.

The world initialize in english means to start something so think of it as the method that starts things off. An initialize method will hold methods that will happen automatically every time an instance of a class is created. This just means if we setting methods inside the class they will have the opportunity to use the default values that have been set or whatever the initialize method contains. I will give a quick example here before I finish explaining what initialize does to help try to make it clear.

 
class Greeter 
 def initialize(name = "world") 
  @name = name 
 end 
 def say_hi 
  puts "Hi #{@name}!"
 end 
 def say_bye 
  puts "Bye #{@name}, come back soon"
 end 
end

So what is happening is we are telling the computer that when we call the say_hi or say_bye method there should be parameters of a name passed. If there aren’t any parameters passed we will say world. If name didn’t have a default value it would just be (name) and below it we have created the instance variable @name which = the value of name. So if you did pass a name it could have been stored in that variable. Here’s how it looks in action.

>> class Greeter 
>>  def initialize(name = "world") 
>>   @name = name 
>>  end 
>>  def say_hi 
>>   puts "Hi #{@name}!"
>>  end 
>>  def say_bye 
>>   puts "Bye #{@name}, come back soon"
>>  end 
>> end
=> nil
>> b = Greeter.new()
=> #
>> b.say_hi
Hi world!
=> nil
>> c = Greeter.new("Kyle")
=> #
>> c.say_bye
Bye Kyle, come back soon
=> nil
>> d = Greeter.new("Kyle","John")
ArgumentError: wrong number of arguments (2 for 1)
	from (irb):16:in `initialize'
	from (irb):16:in `new'
	from (irb):16

I’m creating a new instance of the Greeter class. Every time I do that it’s running the initialize method which is allowing me to pass no parameters and still get Hi world! If I call the new method on greeter then pass “Kyle” I can use the variable I stored that parameter in to say Hi Kyle or in this case Bye Kyle, come back soon. Lastly you can see that it won’t accept more parameters than were defined.

There are a couple other things to know about initialize:
– How do we pass the arguments into initialize?… the .new method takes values it has and passes them into initialize.
– Once we start adding arguments to .new it always expects them… unless we set default values.

– If you are working with an object and what to know what methods have been defined on it you can call .instance_methods(false) … if you just called instance methods it would return all of the ones defined by ancestor classes.

Last thing we are going to mention about methods because you will see it so often is attr_accessor methods. There are three attr methods
attr_accessor – creates a getter a setter
attr_reader – creates a getter
attr_writer – creates a setter

The reason you wouldn’t just use accessor for everything is mostly because of performance you don’t need to have a bunch of methods in your program that will never be called.

Getter and setter methods.
above we created an instance variable of @name so that we could read the name we passed in the arguments but if we want to be able to write the name we need to do something that looks like this.

def name =(str) 
 @name = str 
end

That way our name is set to a string.

so by saying

def name 
 @name 
end

We can read the name and the above lets us write the name. Creating these two types of methods happens so often that ruby gave use attr methods to speed up this process. I’m sure you can tell from my description above which one does which.

If I want to create them all I have to do is call them.

attr_accessor :name (does everything I did above) getter and setter both finished and the instance variable is still available for use so I could also make other methods within my class like.

def greeting 
 "Hello #{@name}" 
end

Then I can do a bunch of different things once I call .new on the class. I can set that instance to a variable g, set the name with:
g.name = “Ron” … then call my new greeting method
g.greeting => “Hello Ron”

Hope that helps clarify some things I will probably be back to make changes to these posts but in the meantime check out the Ruby documentation if anything I say doesn’t make sense.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s