Subjects
PRICING
COURSES
SIGN IN
Start Free Trial
Emma L.
MIT CS Graduate and Senior Web Developer
Tutor Satisfaction Guarantee
Ruby Programming
TutorMe
Question:

What is meta-programming in Ruby?

Emma L.
Answer:

Simply stated, meta-programming in Ruby is writing code that generates more code. For example, let's say we have a class that has a lot of similar functions (I just used two for simplicity): class Cat #@name and @breed have been set earlier def name_cute @name + "meow" end def breed_cute @breed + "meow" end end you could re-write them so they were programmatically generated. class Cat CUTE_METHODS = ["name", "meow"] CUTE_METHODS.each do |cute_method| define_method("#{cute_method}_cute") do instance_variable_get("@#{cute_method}) + "meow" end end In this way, you can avoid code duplication, and potentially reduce maintenance time if these methods are likely to be coupled so tightly.

jQuery Programming
TutorMe
Question:

Why might you want to use event.stopPropagation() in jQuery?

Emma L.
Answer:

Events in jQuery (for instance, click events or mouse events) that are triggered on an object will look for and try to execute an event of that type on that object and all the parent objects. For example, if you have a div called outer with a button called inner inside it - aka: <div class="outer"> <button class="inner"> </button> </div> and these both have click events defined: $(".outer").click(function(){ alert("outer!"); }); $(".inner").click(function(){ alert("inner!"); }); when you click on the button, both events will be called. So, if you click on the button, you'll get an alert that says "inner" followed by an alert that says "outer." If you only want the functionality of the object that the event was called on (in this case, if you only want the "inner" click function to be called) you can modify the function: $(".inner").click(function(event){ alert("inner!"); event.stopPropagation(); }); This will stop the parent classes event functions from automatically being called. So, if you clicked on the button with this code, all you'd see is the alert with "inner" and no subsequent alerts.

Javascript Programming
TutorMe
Question:

How are variables scoped in Javascript, and why is this significant?

Emma L.
Answer:

Javascript has several ways of scoping variables. The scope of a variable refers to where you can use a variable based on when you assigned the variable. For example, if you say: var x = 5 at the beginning of your function, you want to know that you can later use the value x in a statement (say, x + 1) and get the expected result (in this case, 6) Here are a few common way javascipt variables are scoped: Global scope: If you set a variable outside any function, this is considered global scope, and will be retained after a function is done running. For example, if you write: var x = 7; function add_one() { x = x + 1; } function log(){ console.log(x) } If you call add_one() then log(), you will log 8 to the console, but if you call log() then add_one() you will log 7 to the console. This is because any modification to a global variable the add_one function makes is saved globally, and so can be used by all other functions. Local Scope: If you set a variable in a function, then it can only be used by that function. If we modify the earlier example to this: function add_one(){ var x = 7; x = x + 1; } function log(){ console.log(x); } log() will always return undefined, because it cannot access the x that was set in "add_one". Closures: You can define functions within functions, and the inner function can access the variable of the outer function. The variables the inner function has access to are within the closure of that function. Example: function outer() { var secret = 1; function inner() { console.log(secret); } } If you call inner(), this will log 1 to the console. This example is simplistic, but, you can use this method to allow limited, and pre-specified interactions with your variables. The only way a user can interact with the variable secret is by calling the function inner. Contrast this with a global variable, where any function can change the variable, or a local variable, where another function has no access to the inner variables of another function. Block Scoping Does Not Exist: Unlike in other languages, like C or C++, Javascript does not allow block scoping. What this means is this function: function one(){ var x = 8; if(true){ x = 7; } console.log(x); } will log 7. In another language, it would probably log 8. This is sometimes unexpected behavior, as it is more common that the variables modified within a block statement (say, an if statement or while loop) will not be accessible out of that block. This can lead to bugs. This was just a quick overview of a few different types of common scoping issues in javascript. It's important to use the correct type of scoping for the program you are writing, so that it exhibits the expected behavior.

Send a message explaining your
needs and Emma will reply soon.
Contact Emma
Ready now? Request a lesson.
Start Session
FAQs
What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Session" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.