- Comprehensive Ruby Programming
- Jordan Hudgens
- 195字
- 2021-07-02 21:13:33
Method argument code examples
I'm going to start with a very basic example that prints out a user's full name. This is actually a code snippet I took from a real-world code project:
def full_name(first_name, last_name)
first_name + " " + last_name
end
puts full_name("Jordan", "Hudgens")
As you'll see, this prints out my full name.

The syntax for how to pass arguments to a method is to simply list them off on the same line that you declare the method. From here, you can treat the names that you supplied as arguments (in this case, first_name and last_name) as variables in the method.
Now there is also an alternative syntax that we can use that's quite popular among Ruby developers for passing arguments to methods. Here is a slightly updated version of the code example from earlier:
def full_name first_name, last_name
first_name + " " + last_name
end
puts full_name"Jordan", "Hudgens"
Notice how all of the parentheses have been taken away? There is some debate on which option is better. I personally prefer to write less code when I have the chance to, so I prefer the second syntax. However both options will work well.
- Learn Blockchain Programming with JavaScript
- TypeScript入門與實戰
- What's New in TensorFlow 2.0
- Learn to Create WordPress Themes by Building 5 Projects
- Android 7編程入門經典:使用Android Studio 2(第4版)
- 算法精粹:經典計算機科學問題的Python實現
- Servlet/JSP深入詳解
- INSTANT Sencha Touch
- Hands-On Enterprise Automation with Python.
- Hands-On Reinforcement Learning with Python
- Geospatial Development By Example with Python
- jQuery技術內幕:深入解析jQuery架構設計與實現原理
- Python一行流:像專家一樣寫代碼
- 絕密原型檔案:看看專業產品經理的原型是什么樣
- Java Script從入門到精通(第5版)