DEV Community

Madhu Sudhan Subedi
Madhu Sudhan Subedi

Posted on

I am learning ruby

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

3. Function in Ruby

  • Function can be define using 'def' like this
def double(value); value * 2; end
double(2) // results 4
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

Top comments (7)

Collapse
 
katpadi profile image
Kat Padilla

Good luck, fam!

Collapse
 
sudhansubedi profile image
Madhu Sudhan Subedi

Thank you ! Would you like to suggest me 1 book to read to get better knowledge on ruby ?

Collapse
 
welearnednothing profile image
Jeff Carnes

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.

Collapse
 
rpalo profile image
Ryan Palo

Good luck on your ruby journey! :)

Collapse
 
sudhansubedi profile image
Madhu Sudhan Subedi

Thank you ! It's been amazing journey with ruby till now.

Collapse
 
abuzzany profile image
Angel Buzany

Are you thinking write a pos about the difference between lambdas, procs and blocks?
I think It can be a good post

Collapse
 
sudhansubedi profile image
Madhu Sudhan Subedi • Edited

Hmm yeah, I will write posts for it after 5 days. Thanks for the suggestion !