Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. # every thing is an object:
  2. 20.methods
  3. #=> [:to_s, :inspect, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv, :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^, :[], :<<, :>>, :to_f, :size, :bit_length, :zero?, :odd?, :even?, :succ, :integer?, :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :eql?, :remainder, :real?, :nonzero?, :step, :quo, :to_c, :real, :imaginary, :imag, :abs2, :arg, :angle, :phase, :rectangular, :rect, :polar, :conjugate, :conj, :between?, :psych_to_yaml, :to_yaml, :to_yaml_properties, :find_method, :_ori_method, :ri, :system, :spawn, :howtocall, :vi, :vim, :emacs, :nano, :mate, :mvim, :ed, :methods_for, :method_lookup_path, :only_class_ancestors, :mlp, :nil?, :=~, :!~, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__, :instance]
  4. String.class
  5. #=> Class
  6. Class.is_a? Object
  7. #=> true
  8.  
  9. # Lambdas, Iterators and Ranges
  10. [0,1,2,3,4,5].each { |num| puts num } #=> [0, 1, 2, 3, 4, 5]
  11. 0
  12. 1
  13. 2
  14. 3
  15. 4
  16. 5
  17.  
  18. (0..10).select(&:even?).map(&:to_s).join(' ') #=> "0 2 4 6 8 10"
  19.  
  20. # Classes
  21. class Car
  22. attr_reader :color
  23. attr_reader :max_velocity
  24.  
  25. attr_accessor :current_speed
  26.  
  27. def initialize(color, max_velocity)
  28. @color = color
  29. @max_velocity = max_velocity
  30. @current_speed = 0
  31. end
  32.  
  33. def drive(new_speed = max_velocity)
  34. raise "Can't drive that fast!" if new_speed > max_velocity
  35. self.current_speed = new_speed
  36. end
  37.  
  38. def tune(percentage)
  39. @max_velocity += percentage.to_f/100 * @max_velocity
  40. end
  41. end
  42.  
  43. # Modules
  44. module Breaks
  45. def stop
  46. @current_speed = 0
  47. end
  48. end
  49.  
  50. class BetterCar < Car
  51. include Breaks
  52. end
  53.  
  54. toyota = BetterCar.new 'blue', 50
  55. toyota.drive
  56. toyota.current_speed
  57. #=> 50
  58. toyota.stop
  59. toyota.current_speed
  60. #=> 0
  61.  
  62.  
  63. module StringExtensions
  64. def greet
  65. puts "hello " + self
  66. end
  67. end
  68. String.send(:include, StringExtensions) #=> String
  69. "Stefan".greet #=> nil
  70.  
  71. hello Stefan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement