Before get started with Ruby on Rails, I am just learning ruby programming language and I found the following facts about ruby. I am writing basics of ruby programming which I learned today.
1. Variable
- Ruby variables are dynamic typing and duck typing
- Can be define simply assigning value to the variable
count = 10
awesome_count = 20 // correct convention
awesomeCount = 20 // Wrong convention
- Yay ! we do not need any semicolon here
- Common convention for the varible define is snake case not camel case
- 2 Space is common practice instead of 4 space in code
2. Comment in Ruby
- Writing comment in ruby is quite simple and straight forward
- For e.g.
# This is ruby comment
3. Function in Ruby
- Function can be define using 'def' like this
def double(value); value * 2; end
double(2) // results 4
- We do not need return statement to return value too
- In above example single line function and statement are seperated by semi-colon please do not confused on it
- For the multiple line we can write like this
def double(value)
value * 2
end
double(2) // results 4
Top comments (7)
Good luck, fam!
Thank you ! Would you like to suggest me 1 book to read to get better knowledge on ruby ?
I’ve always been a fan of Eloquent Ruby. Unfortunately, it hasn’t been updated in some time. Still might be worth checking out.
The Rails Way is great if you’re getting into Rails.
Good luck on your ruby journey! :)
Thank you ! It's been amazing journey with ruby till now.
Are you thinking write a pos about the difference between lambdas, procs and blocks?
I think It can be a good post
Hmm yeah, I will write posts for it after 5 days. Thanks for the suggestion !