Skip to content

Instantly share code, notes, and snippets.

@ponkore
Created April 11, 2012 10:14
Show Gist options
  • Save ponkore/2358391 to your computer and use it in GitHub Desktop.
Save ponkore/2358391 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Ruby 1st)
irb(main):008:0> (1..10).each { |i| puts "This is sentence number #{i}" }
This is sentence number 1
This is sentence number 2
This is sentence number 3
This is sentence number 4
This is sentence number 5
This is sentence number 6
This is sentence number 7
This is sentence number 8
This is sentence number 9
This is sentence number 10
=> 1..10
irb(main):009:0>
#!/usr/bin/env ruby
while (true) do
rnd = rand(10)
print "Input number(1-10)>"
STDOUT.flush
i = gets()
break unless i
input = i.chomp!.to_i
result = (input > rnd ? "GREATER" : (input < rnd ? "LESS" : "EQUAL"))
puts "Your input number '#{input}' is #{result} (rnd=#{rand})"
end
puts
bash$ ./a.rb
Input number(1-10)>1
Your input number '1' is LESS (rand=9)
Input number(1-10)>1
Your input number '1' is LESS (rand=8)
Input number(1-10)>1
Your input number '1' is LESS (rand=3)
Input number(1-10)>1
Your input number '1' is LESS (rand=5)
Input number(1-10)>3
Your input number '3' is LESS (rand=6)
Input number(1-10)>3
Your input number '3' is LESS (rand=8)
Input number(1-10)>3
Your input number '3' is GREATER (rand=1)
Input number(1-10)>3
Your input number '3' is LESS (rand=9)
Input number(1-10)>3
Your input number '3' is GREATER (rand=1)
Input number(1-10)>3
Your input number '3' is GREATER (rand=0)
Input number(1-10)>3
Your input number '3' is LESS (rand=9)
Input number(1-10)>3
Your input number '3' is LESS (rand=8)
Input number(1-10)>3
Your input number '3' is LESS (rand=7)
Input number(1-10)>3
Your input number '3' is GREATER (rand=1)
Input number(1-10)>3
Your input number '3' is LESS (rand=4)
Input number(1-10)>3
Your input number '3' is LESS (rand=6)
Input number(1-10)>3
Your input number '3' is EQUAL (rand=3)
Input number(1-10)>^D
bash$
irb(main):002:0> puts "Hello, world"
Hello, world
=> nil
irb(main):003:0>
irb(main):009:0> 10.times { puts "My Name" }
My Name
My Name
My Name
My Name
My Name
My Name
My Name
My Name
My Name
My Name
=> 10
irb(main):010:0>
irb(main):001:0> "Hello, Ruby".index(/Ruby/)
=> 7
irb(main):002:0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment