Skip to content

Instantly share code, notes, and snippets.

@raghubetina
raghubetina / euler_p1.rb
Created July 7, 2012 19:16
Project Euler Problem 1
# My solution:
# First, a helper method:
def is_a_multiple_of?(factor, candidate)
return candidate % factor == 0
end
sum = 0 # I read this as:
1.upto(999) do |number| # "From 1 upto 999, for each number,"
if is_a_multiple_of?(3, number) # "If the number is a multiple of 3"
@raghubetina
raghubetina / gist:4161422
Created November 28, 2012 13:46
Client-side Scripting
<h1>Chicago</h1>
<h1>San Francisco</h1>
<h1>New York</h1>
<h1>Boston</h1>
<a href="http://www.google.com" id="button">Abracadabra!</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
@raghubetina
raghubetina / gist:4162076
Created November 28, 2012 15:45
JavaScript Example
<h1>Chicago</h1>
<h1>San Francisco</h1>
<h1>New York</h1>
<h1>Boston</h1>
<a href="http://www.google.com" id="button">Abracadabra!</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
first = "Meryl"
last = "Charleston"
birthday = "June 9"
puts "#{first} #{last} was born on #{birthday}."
first = "Rachel"
last = "Conley"
birthday = "October 31"
require 'open-uri'
require 'json'
require './student.rb'
s1 = Student.new
s1.name = "Raghu Betina"
s1.photo_url = "https://graph.facebook.com/rbetina/picture"
s1.section = "am"
s1.twitter = "rbetina717"
class Student
attr_accessor "name"
attr_accessor "photo_url"
attr_accessor "section"
attr_accessor "twitter"
def introduce
return "#{@name} (@#{@twitter}) is in the Web Dev #{@section.upcase} class."
end
end
#################################
# Automating repetition
#################################
# .times is a way to loop in Ruby.
# This is how you print "howdy" five times:
# 5.times do
# puts "howdy"
# end
# Hard Challenge: Based on the data stored in the shopping_cart, sales_tax, and params variables below, write code that prints out the customer's total, including estimated sales tax, based on the shipping address they entered. Your code should work even if the values of the data change.
# Your output should look like this:
# Your total with tax is $4455.54.
shopping_cart = [
{'name' => "iPad 2", 'price' => 499, 'quantity' => 2},
{'name' => "iMac 27", 'price' => 1699, 'quantity' => 1},
{'name' => "MacBook Air 13", 'price' => 1299, 'quantity' => 1}
]
5.times do |i| for (var i = 0; i < 5; i++) {
puts "howdy #{i}" alert("howdy " + i.toString());
end };
(10..20).step(2) do |i| for (var i = 10; i <= 20; i = i + 2) {
puts "howdy #{i}" alert("howdy " + i.toString());
end };
a = ["apple", "orange", "banana"] var a = ["apple", "orange", "banana"];
a.each do |fruit| for (var i = 0; i < a.length; i++) {
@raghubetina
raghubetina / friendbc.rb
Last active December 16, 2015 01:39
Example of reading from the Graph API
require 'open-uri'
require 'json'
your_access_token = "put your access token from the Graph API Explorer here"
url = "https://graph.facebook.com/me/home?fields=from,picture,link,source,name,description,type&access_token=#{your_access_token}"
result = JSON.parse(open(url).read)
posts = result["data"]