Skip to content

Instantly share code, notes, and snippets.

@muhammadyana
Last active June 8, 2017 04:08
Show Gist options
  • Save muhammadyana/dbe8738ce8f6be3e5a41b1fb60a8995a to your computer and use it in GitHub Desktop.
Save muhammadyana/dbe8738ce8f6be3e5a41b1fb60a8995a to your computer and use it in GitHub Desktop.
ruby-basic-progamming
def Hallo(name, age)
tmp = "Hallo #{name.upcase} your age is \n #{age}"
end
language = ["java", "html", "css", "ruby"]
anotherMethod = %w{java html css ruby go}
#puts language.inspect #show all element in array
#puts anotherMethod[0]
#puts Hallo("yana", 24)
a = %w{ ant bee cat dog elk }
#puts a[0] # => "ant"
#puts a[3] # => "dog"
has = {
'name' => 'john',
'age' => 'doe',
'weight' => '30'
}
#p has['name']
#hashes
inst_section = {
@cello => 'string',
:clarinet => 'woodwind',
:drum => 'percussion',
:oboe => 'woodwind',
:trumpet => 'brass',
:violin => 'string'
}
# p inst_section[@cello]
# puts inst_section[:drum]
#p inst_section.inspect
#puts inst_section.inspect
date = Time.now
if date.saturday?
p "keep cleaning"
elsif date.sunday?
puts "Please relax"
else
puts "Go Work"
end
# line = gets
# if line =~ /Perl|Python/
# puts "Scripting language mentioned: #{line}"
# end
#p date.daturday?
# line = gets
# newline = line.sub(/java/, 'Ruby') # replace first 'Perl' with 'Ruby'
# puts newline
# newerline = newline.gsub(/css/, 'framework') #replace very css with framework
# puts newerline
# def call_block
# puts "Start of method"
# yield
# yield
# yield
# puts "End of method"
# end
# call_block {puts 2*6}
# Block = anon method
def who_say_what
yield
yield
end
#who_say_what{ puts "in"}
#|var1, var2| make new variable in array or iteratur
# who_say_what {|ppl, phrase| puts "#{ppl} say #{phrase}"}
# def say_hai
# if @names.nil?
# puts "..."
# elsif @names.respond_to?("each")
# # @names adalah list, iterate!
# @names.each do |name|
# puts "Hello #{name}!"
# end
# else
# puts "Hello #{@names}!"
# end
# end
#say_hai
#anotherMethod.each {|data| puts data} #iterator
# anotherMethod.each do
# |a| p a #iterator
# end
# while line = gets
# puts line
# end
class BookInStock
attr_reader :isbn
attr_accessor :price
def initialize(isbn, price)
@isbn = isbn
@price = Float(price)
end
# def to_s
# "ISBN : #{@isbn} and Price : #{@price}"
# end
end
# b1 = BookInStock.new("isbn1", 3)
# puts b1
# b2 = BookInStock.new("isbn2", 3.14)
# puts b2
# b3 = BookInStock.new("isbn3", "5.67")
# puts b3
book = BookInStock.new("isbn1", 33.8)
#puts "ISBN : #{book.isbn}"
# puts "You gave #{ARGV.size} arguments"
# p ARGV
name ="Timm"
objectId = 70212733196880
# #puts anotherMethod.class
# if objectId == $name.object_id
# puts "yeah"
# else
# puts ":("
#
# end
#puts name.freeze
# another array
b = Array.new
b[0]="Yana"
b[1]="Mulyana"
#p anotherMethod[-3,0]
#p language[0][0]
# for i in 0..4
# word = language[i][2]
# count = language[i][1]
# puts "#{word}: #{count}"
# end
# language.each do |word, count|
# puts "#{word} : #{count}"
# end
# language.each {
# |word, count|
# p word * 3
# }
sum = 0
[1, 2, 3, 4].each{ |value|
square = value * value
sum += square
}
#puts sum
sum2=0
[2, 2].each {
|val|
tmp = val * val
sum2 += tmp
p tmp
}
puts sum2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment