Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. # CareerFoundry Exercise 21
  2.  
  3. # this is a comment
  4.  
  5. # puts is "put string".
  6. # this will output without any "" on the terminal because it is already a string value
  7. puts "hello, world!"
  8. puts "42"
  9.  
  10.  
  11. # p is for "print"
  12. p "hello, world again using p!"
  13. p 42 + 200
  14. p 84 * 4
  15.  
  16. # METHODS
  17. def greeting(name) # define the method using the def keyword. this is like a function in js
  18. p "hello" + " " + name # code goes here
  19. end # always end/close the method using the "end" keyword
  20.  
  21. greeting("Ekel") # call the method using its name
  22.  
  23.  
  24. # METHODS USING VARIABLES
  25. my_name = "Ekel Duran Echevarria" # define variable. small caps, snake_case preferred
  26. def greeting_by_variable(my_name)
  27. p "hello" + " " + my_name
  28. end
  29. greeting_by_variable(my_name)
  30.  
  31.  
  32. # METHOD WITH USER INPUT
  33. def greeting_with_user_input
  34. puts "Please enter your name"
  35. username = gets.chomp # gets = "get string". chomp will remove new line
  36. puts "Hello, " + username
  37. end
  38. greeting_with_user_input # call method. no arguments necessary.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement