You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To use puts to simply writes onto the screen whatever comes after it.
To use gets to get words form the keyboard.
To use chomp to take off the line break.
puts1 + 2# same as the above oneputs3# output what inputputsgets# comparition with chmopletname=getsputs'Hello, ' + name + ', is that OK?'letname=gets.chompputs'Hello, ' + name + ', is that OK?'
let='variables'putslet# => variables# variables can point to any kind of objectlet=5 * (1 + 2)putslet# => 15
Conversion
let1=2let2='5'putslet1 + let2# It will go wrongputslet1.to_s + let2# => 25puts'15'.to_f# => 15.0puts'99.99'.to_i# => 99
Methods
# In Ruby, operators like +, * is just a shortcut way of .+, .*.puts'What\'s ' + 'up?'puts'What\'s '.+ 'up?'# puts is really also a shortcut wayputs'What?'self.puts'What?'puts`pig` * 5# Ruby cannot run puts 5 * `pigs`, because 5 cannot identify to copy 'pig' for 5 times.# Some string methodslet1='stop'putslet1.reverse# => potslet2='Aleen'puts'There\'re ' + let2.length.to_s + ' characters in ' + let2letters='aAbBcCdDeE'putsletters.upcase# => AABBCCDDEEputsletters.downcase# => aabbccddeeputsletters.swapcase# => AaBbCcDdEeputsletters.capitalize# => Aabbccddeelet3='Alien'lineWidth=50putslet3.center(lineWidth)putslet3.ljust(lineWidth)putslet3.rjust(lineWidth)# Random Methodsputsrand(101)# means to random from 0 to 100putssrand(1776)# in the same rand serials => 24putssrand(1776)# => 24# Math ObjectputsMath::PIputsMath::EputsMath.cos(Math::PI / 3)# => 0.5000000000000001