Download presentation
Presentation is loading. Please wait.
Published byJohn Curtis Modified over 9 years ago
1
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Writing Methods
2
CIT 383: Administrative Scripting Topics 1.Defining a Method 2.Return Values 3.Arguments 4.Local Variables 5.Meaningful Names
3
CIT 383: Administrative Scripting Defining a Method Methods are defined with the def keyword. def name(arg1, arg2)... end Example def circle_area(radius) Math::PI*radius*radius end
4
CIT 383: Administrative Scripting Return Values Methods return value of last expression area = circle_area(2.1) The return statement causes function to return immediately with specified value. def factorial(n) return 1 if n == 1 n * factorial(n-1) end
5
CIT 383: Administrative Scripting Methods can return multiple values def celcius2farenheit(celcius_temp) if celcius_temp < -273.15 return -1, “Impossible temperature” else return 0, 32 + 1.8*celcius_temp end error, farenheit_temp = celcius2farenheit(celcius) if error == -1 puts “Conversion error: #{farenheit}” else puts “The temperature is #{farenheit} degrees F.” end
6
CIT 383: Administrative Scripting Arguments Methods take zero or more named arguments def noargument def oneargument(arg1) def twoarguments(arg1, arg2) Variable number of arguments Last argument must be prefixed by a * def csv_list(*values) values.join(“, “) end comma_sep_list = csv_list(4,5,6)# “4, 5, 6”
7
CIT 383: Administrative Scripting Default Arguments Optional default values for arguments def prefix(string, len=1) string[0, len] end If default exists, do not need to specify arg prefix(“ruby”, 3) # “rub” prefix(“ruby”, 1) # “r” prefix(“ruby”) # “r”
8
CIT 383: Administrative Scripting Default Arguments Default values can be expressions def suffix(str, index=str.size-1) str[index, str.size-index] end Suffix returns final substring starting at index suffix(“ruby”, 1) # “uby” suffix(“ruby”, 3) # “y” suffix(“ruby”) # “y”
9
CIT 383: Administrative Scripting Local Variables Local variable: A variable defined within a method, block, or loop. Scope: Where a variable can be used. The scope of a local variable starts from its definition and continues to the keyword (usually end ) terminating the block, method, or loop. Global variable: A variable accessible anywhere in the program. A global variable’s name must begin with $.
10
CIT 383: Administrative Scripting Local Variables $ irb > def set_variable(value) >var = value > $global_var = value > puts “var set to #{var}” > end > > set_variable(10) > puts $global_var 10 > puts var NameError: undefined local variable or method `var' for main:Object
11
CIT 383: Administrative Scripting Method Names Method names begin with a letter –Method names may contain letters, numbers, _ –Method names may end with a ? or ! Special method names –Setter method names end with an = –Operator methods Named for and act like operators. Examples: +, -, **, >=, &&
12
CIT 383: Administrative Scripting Meaningful Names Use intention-revealing names –Bad: d = 8 # d is days since last login –Better: days_since_login = 8 Avoid disinformation –Ex: give plural objects plural names –character = ?a –characters = Array.new
13
CIT 383: Administrative Scripting Meaningful Names Use searchable names –Long enough names to search for. –Use named constants instead of magic numbers. –Restrict single-letter names to local variables. Use pronounceable names –Easier to communicate with others.
14
CIT 383: Administrative Scripting Meaningful Names Use verbs in method names –save –save_quota_data Pick one word per concept –delete –remove –expunge –kill
15
CIT 383: Administrative Scripting Writing Methods Do one thing –A method should do one and only one task. Indent –Indent the body of a method by one tab. –Keep level of indenting low; if you find more than two levels of indentation within a method, break it into multiple methods. Keep it small –Always less than one page. –2-12 lines is typical of a good function.
16
CIT 383: Administrative ScriptingSlide #16 References 1.Michael Fitzgerald, Learning Ruby, O’Reilly, 2008. 2.David Flanagan and Yukihiro Matsumoto, The Ruby Programming Language, O’Reilly, 2008. 3.Hal Fulton, The Ruby Way, 2 nd edition, Addison- Wesley, 2007. 4.Robert C. Martin, Clean Code, Prentice Hall, 2008. 5.Dave Thomas with Chad Fowler and Andy Hunt, Programming Ruby, 2 nd edition, Pragmatic Programmers, 2005.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.