Ruby tutorial! [R JAM]
Hello Peoplessss!!!
So lately I have been seeing a lot of full course tutorials, this isnt full course but yeah.
I have been busy with school a lot but now I can write this tutorial for YOU :)
What is Ruby?
Ruby is yet another programming language and it is in a way similar to Python, if you know Python then Ruby will be a breeze.
Some History
Ruby is an interpreted, high-level, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. Ruby is dynamically typed and uses garbage collection. According to the creator, Ruby was influenced by Perl, Smalltalk, Eiffel, Ada, BASIC, and Lisp (cannot believe it! When it is super similar to Python)
Remember erroring is coding Remember coding is banging your head through a wall and staring at your computer doing nothing
Hello, Ruby! (Lesson 1)
Ruby is pretty fun and really easy to learn!
Comments
In Ruby comments are the same exact as Python! Use a hashtag (#) to make comments!
# Hello, this is a comment # This won't get ran! # sdfasdkfhasdlfhasdf;l # That won't error # Pretty epic, right?!
Hello, Ruby
The basic way to output Hello, Ruby! is:
puts 'Hello, Ruby'
So puts is the print function in Ruby, pretty cool right?
But you can also use print:
print 'Hello, Ruby'
AND printf (Using puts is best for basic stuff though)
printf 'Hello, Ruby!
The basic way to output Hello, Ruby! is:
puts 'Hello, Ruby'
So puts is the print function in Ruby, pretty cool right?
But you can also use print:
print 'Hello, Ruby'
AND printf (Using puts is best for basic stuff though)
printf 'Hello, Ruby!
You should specify what's the difference (I think puts does a new line automatically)
~ @Bookie0
Yes puts does make a new line at the end, but you will have to google what the other two do. :)
Playing with Strings!
So now that you know how to output strings we can play around a little bit.
puts "Ruby is cool!" puts "It is similar to Python!" puts "Ruby was made by a programmer, like you!" puts "I shall now serenet for benana\n"
puts "Hello, my name is dude \nMy friends name is dude too"
If you are learning a new language I always suggest just playing around with the keywords and seeing what they do. You can do much more stuff like print "Hi my name is E and I am made in Ruby!"
If you want to use \n
You are going to have to put ""s I suggest using ""s just so you can be ready for anything that happens, because who knows, when you are about to be writing a super styled Program!
Now since puts
, print
, and printf
are built-in(by built-in I mean not in the std file) they do not need parenthese(())
Variables in Ruby!
Now have you made one program without variables that was more than 50 + lines long? My guess is no. So variables are pretty epic! They are an important part in the programming culture, otherwise programming wouldn't be programming.
Variables, in Ruby are similar to Python, actually some would say they are the same. So if you know python you already know how to make a variable!
name = "jeff1234"
Some sample code:
name = "ruby" date = "2020/3/11" time = 558 pmoram = "pm" age = 50.555 epic = true
In Ruby you do not really need to worry about data types, because, as you see above it accepts ints in strings! So not much different than Python...but still :D.
How to Print Variables in Ruby!
So, what is variables without printing them?
Well just random things taking up space on your computer.
If you don't do anything with the variables then its like, not a variable.
So, lets have some fun with variables!!!!
A simple variable output would be:
life = ":(" puts life
The code above would output: :(
Hmmm what about putting strings and variables together Eli?
Great question!
You would simply put puts with parenthese! Then, put your variable name with a plus!
Some example code:
e = "e" puts "e + "+e+" = e"
Which the output would then be:
e + e = e
the forces have spoken
If I do say so myself, that is a waste of code! Make the variable e
and only use it once... it just doesn't feel right! We can fix this!
e = "e" puts e+" + "+e+" = "+e
This again, will output: e + e = e
Too much power in one program!!!
Edit: Parentheses are totally irrelevant in Ruby
Yayayaya now more fun with strings!
These are just some tips that can help you in your future programs!
Print a variable UPPERCASE
name_test = "Hello, this is just a test" puts name_test.upcase()
This is actually fun to do in some programs, like return strings in uppercase.
Print a variable in lower case
name_test = "HELLOOOOOOOOOOOOOOO, this is just a test" puts name_test.downcase()
Find something in the string
name = "Jeff, and bob." puts name.include? "bob" # outputs true #Will output true or false.
So, this basically searches the string and then returns a value(true/false) if the thing that you made it search for is in the string
That is pretty much it for the basic stuff if you want to find more go here
Hmm.... The water is deeper...Lesson 2
So this chapter is mostly about loops, if, else, etc;
Mini-part: Operators!
Aritmetic ops!
The Ruby arithmetic operators are:
+
For adding numbers-
For subtracting numbers/
For dividing numbers*
Multiplication for numbers%
Modulus**
Exponent
Comparison Ops!
There are a pretty big amount of these.
==
Checks if the value of two operands are equal or not, if yes then condition becomes true.(a == b) is not true.
!=
Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.(a != b) is true.
>
Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(a > b) is not true.
<
Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(a < b) is true.
>=
Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(a >= b) is not true.
<=
Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(a <= b) is true.
<=>
Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second.(a <=> b) returns -1.
===
Used to test equality within a when clause of a case statement.(1...10) === 5 returns true.
.eql?
True if the receiver and argument have both the same type and equal values.1 == 1.0 returns true, but 1.eql?(1.0) is false.
equal?
True if the receiver and argument have the same object id.if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true.
Learn more here
Woooooohoooooooo that was a lot
I know some of those were not that simple, but you will maybe need them sooner or later.
Continuing to If Statements
So, if statements are pretty cool!
It is pretty fun to work with if statements in programming.
The if
statement is kind of different than Python.
It doesn't have a colon (:
) and at the end of the statement you have to put an end
If you do not quite understand this, here is some code:
variable = "e" if variable == "e" puts "E" end
So What Does This do?
This program makes a variable assigned to 'e', and then if the variable is equal to (==
) 'e' then, print 'E', after that end.
I hope that was a good explanation sorry if it was a bad one
Ruby Else If
Else If the epic. YAY MULTIPLE IFS BUT WITH A DIFFERENT NAME
Make an else if statement in ruby by:
elsif
Some code:
pie_in_life = true if pie_in_life == true puts "Yay :)" elsif pie_in_life == false puts "Awwww :(" end #Outputs: Yay :)
Do not put end
before the next else/elsif/if statement otherwise... ERROR TIME BABY
Hmm did I say else before we got to the next part well here is the next part ;)
Else
The beautiful else :) Its else. Explanation:
If the first if is true then go through the code, if not go to the else/elsif.
pie_in_life = true if pie_in_life == true puts "Yay :)" elsif pie_in_life == false puts "Awwww :(" else puts "pie confuzelled" end
So if we change pie_in_life
to "squid"
(haha free ping go brrrrrrrr @DynamicSquid since he/she is like copy write of the squid) then we will get:
pie confuzelled
See Ruby is fun! No colons, no semi-colons, nothing that teaches you anything about other languages and good practice! As I said a great language! But to be honest Ruby is really cool. So keep using it if you want a job!
Loops
Yay loops are fun you can print pie is epikk
forever to the console and other stuff like that.
The loop syntax:
while <condition> puts "Epicc" end
Or with some actual stuff
while true puts "pie is epikkkk" end
Syntax:
for variable_name[, variable...] in expression [do] # code to be executed End
for
: A special Ruby keyword which indicates the beginning of the loop.
variable_name
: This is a variable name that serves as the reference to the current iteration of the loop.
in
: This is a special Ruby keyword that is primarily used in for loop.
expression
: It executes code once for each element in expression. Here expression can be range or array variable.
do
: This indicates the beginning of the block of code to be repeatedly executed. Do is optional.
end
: This keyword represents the ending of ‘for‘ loop block which started from ‘do‘ keyword.
Example 1:
# Ruby program to illustrate 'for' # Loop using range as expression i = "e" # using for loop with the range for p in 1..5 do puts i end
Output:
e e e e e
Explanation: Here, we have defined the range 1..5. Range Operators create a range of successive values consisting of a start, end, and range of values in between. The (..) creates a range including the last term. The statement for a in 1..5 will allow a to take values in the range from 1 to 5 (including 5).
This is not the end of loops, there are some more, but I wouldn't know how to explain them as good as some other people, so go here to find out more about loops
Input/Projects!
So you have read this far... that means I am doing something good right?! So... lets get going on input! Input... the greatest thing ever!
INPUT
So input. The greatest thing to play around with. In Ruby input is gets
. I will be explaining the way I use, which I think is the easier way.
puts "What is your name?" name = gets puts("Hello,"+name)
Pretty easy right?
Now we can move on to some other fun stuff :)))))
Mini - Lesson Adding in Ruby
Simply add like this:
four = 2+2 puts four puts 21+21 puts 2
FUN :) You can also do it with all the other operators I showed you!
Project 1: Building a cool Madlibs generator!
Yay Madlibs, everyone loves Madlibs. You put words together with poems/songs/etc;
Lets start coding!
puts "Enter a person" person = gets puts "Enter verb" verb1 = gets puts "Enter a place" place1 = gets puts ("This "+person+" "+verb1+" to "+place1)
Now this will come out a little weird, but it is still SUPER funny
This dude run to benana shop
(thanks to @RayhanADev supporting me when I made up the ultimate benana.)
Of course, you can always add nouns, pronouns, etc; But this is proabaly the best project ever for now. SIMPLE IS FUN ;)
Edit: Functions
Oh wow, I forgot functions!
Functions are really important in programming because you can execute 50 lines of code with just saying hi("e")
Functions in Ruby:
def <funcname>(<varname>) puts("Hello,"+varname+"I am jeff") end #Calling a function <funcname> param1, param2 #or <funcname> (param1, param2)
A real example would be:
def hello(hi) puts("Hi"+hi) end hello("e")
Easy right? The function keyword is the same exact as python.
Some cool fax!
It took me four days to make this tutorial
This has 353 lines!
And @RayhanADev for proofreading!
YAY
CREDITS:
https://ruby-doc.org/docs/ruby-doc-bundle/Tutorial/part_02/user_input.html
https://www.tutorialspoint.com/ruby/ruby_operators.htm
https://www.tutorialspoint.com/ruby/ruby_loops.htm
And all the people who read this
Final comment
I know there is WAYYYYY more to a language than this. But this is the basics to get you started
GOODBYE :D
and stay safe :)
Advanced topics coming soon!
Great tutorial on Ruby, you could probably go over some more complex things
why is repl.it talk suddenly being flooded with tutorials
@programmeruser yeah, working on that tbh. Just finished functions and updated it. and blame @EpicGamer007 but tbh I kinda like learning new things
I don't know but I think I started this wave of full tutorials lol
@EpicGamer007 yes u did cause that java tutorial. Now you started a whole new life of things!
@elipie lol. time to read this! seems pretty cool
@EpicGamer007 thanks, after you read, please leave any suggestions :D
@elipie I suggest going over more advanced concepts and libraries but cool!
@EpicGamer007 hmm librarys yeah... I should add that, I was going to add librays and hashing but I still am confused on some parts when I have been developing in ruby for like 10 months
@EpicGamer007 tbh I think programming is nothing but confusing
@elipie ok cool. good luck learnin
@EpicGamer007 OH NOES I GOTTA ADD FUNCTIONS
@elipie ohes
I know basic Ruby now xD! Nice tutorial, I wonder who proofread it xD.
@RayhanADev i give credit ;) at cool fax part
great tutorial. very useful as well. just completed the whole thing. you should make a continuation! I know you haven't got many upvotes but to be honest it would help many people Like me. totally not asking this just for myself
@ThisUserTaken ok! I wills start working on some things, any suggestions?
@elipie maybe, a tutorial on a project, like in the python books? just a overview of all the things in ruby.
@ThisUserTaken ok. im currently working on classes and arrays in ruby right now ;D
Noice.
ffffffiiiiiiiiiiinnnnnnnnnnaaaaaaaaaaalllllllllllllyyyyyyyyyyyyyyyyyyyy a beginner tutorial of ruby
The basic way to output Hello, Ruby! is:
puts 'Hello, Ruby'
So puts is the print function in Ruby, pretty cool right?
But you can also use print:
print 'Hello, Ruby'
AND printf (Using puts is best for basic stuff though)
printf 'Hello, Ruby!
you should specify what's the difference (I think puts
does a new line automatically)
also:
he/she is like copy write of the squid)
or they lol....
Nice! One thing I'd like to correct:
Hmmm what about putting strings and variables together Eli?
Great question!
You would simply put puts with parenthese! Then, put your variable name with a plus!
The parentheses part is irrelevant. This works just fine:
e = "e" puts "e + "+e+" = e"
@fuzzyastrocat hmm yeah, I just like putting parenthese, but I will fix that, thanks for the suggestion!
@elipie No problem! Once again, great tutorial!
@fuzzyastrocat thanks!
@elipie Me and @Fuzzyastrocat could do the Rust tutorial, I don't know about @jakman, but his account has been dormant for about a month
@firefish I'd rather do a Haskell tutorial (or maybe Elixir or Nim), but I wouldn't be opposed to Rust.
@fuzzyastrocat I don't know haskell or elixir, and the only thing i've written in Nim is the game of NIM
@firefish Well that's ok, I just meant in general so I'll probably do one of those by myself, but if/when I have time I wouldn't be opposed to a Rust tutorial.
@fuzzyastrocat yeah, we are just doing a bunch of R tutorials, wait I have an idea. @firefish and @fuzzastrocat I am going to post this on ask, as like a 'jam' or 'series' prize is nothing. And then people would just come right on in, and then after that we could do a different letter, and another letter, etc;
@elipie Ooh, you should do a tutorial for R
lang next! EDIT: Oh wait, you already thought of that
And yeah, that' be cool.
@firefish Also I know Perl, I don't know what Raku changes though
@fuzzyastrocat I heard Raku was better than Parrot...
@firefish Doesn't Raku run on Parrot?
@fuzzyastrocat after some research, you are right, my blasted sources are wrong
@fuzzyastrocat I'm-a just give my crates a good ol' rub
@firefish Lol (intended for the last comment, but actually applicable to both I guess)
@fuzzyastrocat we just need to know everything until about like Loops
@fuzzyastrocat Comment Fuser 3000
@elipie Everything about what?
@fuzzyastrocat @firefish go here cause that explains it better
When I just look forward through this tutorial, I thought you are talking about Rust lmao.
@Wumi4 lmao