Fizz Buzz in Ruby for dummies

Spoiler alert: I am a true novice. Tasked with figuring out fizz buzz in ruby for a class and while I have found more than a few versions of code that solve the problem, my understanding is so rudimentary that I cannot figure out how these examples truly work. First question(refer to spoiler alert if … Read more

Difference between rake db:migrate db:reset and db:schema:load

db:migrate runs (single) migrations that have not run yet. db:create creates the database db:drop deletes the database db:schema:load creates tables and columns within the existing database following schema.rb. This will delete existing data. db:setup does db:create, db:schema:load, db:seed db:reset does db:drop, db:setup db:migrate:reset does db:drop, db:create, db:migrate Typically, you would use db:migrate after having made changes to the schema via new migration … Read more

What does @@variable mean in Ruby?

A variable prefixed with @ is an instance variable, while one prefixed with @@ is a class variable. Check out the following example; its output is in the comments at the end of the puts lines: You can see that @@shared is shared between the classes; setting the value in an instance of one changes the value for all other instances of that class and … Read more

Determining type of an object in ruby

The proper way to determine the “type” of an object, which is a wobbly term in the Ruby world, is to call object.class. Since classes can inherit from other classes, if you want to determine if an object is “of a particular type” you might call object.is_a?(ClassName) to see if object is of type ClassName or derived from it. Normally type checking … Read more

p vs puts in Ruby

p foo prints foo.inspect followed by a newline, i.e. it prints the value of inspect instead of to_s, which is more suitable for debugging (because you can e.g. tell the difference between 1, “1” and “2\b1”, which you can’t when printing without inspect).

Rails: I installed Ruby, now “bundle install” doesn’t work

Bundle with Ruby 2.1.6 You just need to install bundler : This will be installed in a gemset specific to ruby-2.1.6, so it won’t interfere with anything you installed with ruby-2.3.0. You can use to install the required gems. Trying with Ruby 2.3.0 Alternatively, you could just try the example you downloaded with ruby-2.3.0 by … Read more

Ruby: kind_of? vs. instance_of? vs. is_a?

kind_of? and is_a? are synonymous. instance_of? is different from the other two in that it only returns true if the object is an instance of that exact class, not a subclass. Example: “hello”.is_a? Object and “hello”.kind_of? Object return true because “hello” is a String and String is a subclass of Object. However “hello”.instance_of? Object returns … Read more