Guest User

Untitled

a guest
Jun 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. # Hello, world!
  2. "Hello, world!".puts()
  3.  
  4. # Assignment
  5. number = 42
  6. opposite = true
  7.  
  8. # Conditions
  9. number = -42 if opposite
  10.  
  11. # Lists (stored as immutable singly-linked lists)
  12. list = [1, 2, 3, 4, 5]
  13.  
  14. # Tuples (think of them as immutable arrays)
  15. tuple = (1, 2, 3, 4, 5)
  16.  
  17. # Atoms (known as symbols to Ruby people)
  18. # Think of them as an open-ended enumeration
  19. atom = :up_and_atom
  20.  
  21. # Dicts (also known as hashes to Ruby people)
  22. dict = {:foo => 1, :bar => 2, :baz => 3}
  23.  
  24. # Strings (unlike Erlang, Reia has a real String type!)
  25. string = "I'm a string! Woohoo I'm a string! #{'And I interpolate too!'}"
  26.  
  27. # Ranges
  28. range = 0..42
  29.  
  30. # Funs (anonymous functions, a.k.a. lambdas, procs, closures, etc.)
  31. # Calling me with plustwo(40) would return 42
  32. plustwo = fun(n) { n + 2 }
  33.  
  34. # Modules (collections of functions)
  35. # Calling Plusser.addtwo(40) would return 42
  36. module Plusser
  37. def addtwo(n)
  38. n + 2
  39. end
  40. end
  41.  
  42. # Classes (of immutable objects. Once created objects can't be changed!)
  43. class Adder
  44. # Reia supports binding instance variables directly when they're passed
  45. # as arguments to initialize
  46. def initialize(@n); end
  47.  
  48. def plus(n)
  49. @n + n
  50. end
  51. end
  52.  
  53. # Instantiate classes by calling Classname(arg1, arg2, ...)
  54. # For you Ruby people who want Classname.new(...) this is coming soon!
  55. fortytwo = Adder(40).plus(2)
  56.  
  57. # Function references can be obtained by omitting parens from a function call,
  58. # like JavaScript or Python
  59. numbers = [1,2,3]
  60. reverser = [1,2,3].reverse
  61.  
  62. # Function references can be invoked just like lambdas
  63. reversed = reverser() # reversed is now [3,2,1]
  64.  
  65. # You can add a ! to the end of any method to rebind the method receiver to
  66. # the return value of the given method minus the bang.
  67. numbers.reverse!() # numbers is now [3,2,1]
  68.  
  69. # List comprehensions
  70. doubled = [n * 2 for n in numbers] # doubled is [6,4,2]
Add Comment
Please, Sign In to add comment