Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Last active August 29, 2015 13:56
Show Gist options
  • Save mathildathompson/9181961 to your computer and use it in GitHub Desktop.
Save mathildathompson/9181961 to your computer and use it in GitHub Desktop.
simple_ruby

##RUBY

-Type irb into the command line to get into irb;

  • 5 - this is an integer;
  • 5.0 - this is a floating point number;
  • "This is a string"
  • 'I am also a string'
  • "holly's" (this is why we need both single and double quotes)
  • ctrld (takes out out if you make a mistake)
  • 'that is so "ironic"'
  • "that is so "ironic"" (the backslash will ignore the special meaning of apostrophe as ending of a string);

VARIABLES

  • user = 'Juan' (a string inside a variable called user, = is assignment);

  • user == 'Juan' => true (comparison, checking if the user variables == 'Juan');

  • mentor = 2003 mentor = '2003' (Ruby is a dynamically typed language, you can put anything into a variable);

  • stupid = "Groucho Harpo Chico Zeppo" * 1000001

  • stupid = 6 (Ruby will clean up the memory, all of the memory used previously for the 'Groucho Harpo Chico Zeppo' string will be cleaned);

  • nil (another type that means there is nothing there);

  • '33'.to_i => 33 (calling a method on the string object 33 to covert the string into an integer);

  • 47.to_s => '47' (calling a method on the integer 47 to convert it into a string);

  • a = "33"

  • a.class => string (calling the class method on the object)

  • 3.class => Fixnum

  • 3.14.class => Float

  • first = "Groucho"

  • last = "Marx"

  • fullname = first + " " + last => "Groucho Marx"

  • price = 1.99

  • item = 'bananas'

  • quantity = 12

  • description = "#{quantity} #{item} at $#{price}" (REMEMBER WHEN USING VARIABLE INTERPOLATION YOU NEED TO USE DOUBLE QUOTES)

  • "2 + 2 = #{2 + 2}" => "2 + 2 = 4"

  • "2 to the power of 50 = #{2 **50}" => "to to the power of 50 = 11238999068426254"

  • To run a Ruby file in the terminal cd into the folder that the file is in and then type ruby filename.rb

  • Cmdb in sublime to run the code (you will see a window open at the bottom of sublime)

  • #I am a comment (put a hash tag in front of code to comment it out)

  • ['hello', 'I', 'am', 'an', 'array', 'of', 'strings']

  • brothers = ['Groucho', 'Harpo', 'Chico', 'Zeppo']

  • brothers << "Gummo" => ['Groucho', 'Harpo', 'Chico', 'Zeppo', 'Gummo']

  • brothers.pop => ['Groucho', 'Harpo', 'Chico', 'Zeppo'] (pop removes the last item from the array)

  • brothers.each {|brother| puts "#{brother} Marx"} => Groucho Marx, Harpo Marx, Chico Marx, Zeppo Marx, Grummo Marx

HOMEWORK

  • RUBYMONK
  • GIT/GITHUB tutorial;
  • PRACTISE PUSH PORTFOLIO;
  • QUESTIONS FROM PRE-WORK;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment