(Ruby の初歩)
http://www.sw.it.aoyama.ac.jp/2019/Projects2/lecture3.html
© 2019 Martin J. Dürst 青山学院大学
about_CSS.html.
Create a stylesheet named my_style.css, and link it to
about_HTML.html and about_CSS.html.my_style.css to Moodle (deadline: today at 18:20; explicit
submission no longer necessary)..html files: class_Integer.html,
class_String.html,
class_Array.htmlmy_rubydoc.css.my_rubydoc.css.class values
in the file with class selectors.my_rubydoc.css to Moodle (deadline: Wednesday April 17,
22:00).You can use your stylesheet in the Ruby documentation on your notebook computer!
| C | Ruby |
|---|---|
// greatest common denominator
int gcd(int a, int b)
{
int r;
if (a > b)
r = gcd(a-b, b);
else if (a < b)
r = gcd(b-a, a);
else
r = a;
return r;
}
|
# greatest common denominator
def gcd a, b
if a > b
r = gcd a-b, b
elsif a < b
r = gcd b-a, a
else
r = a
end
r
end
|
Integer: 3, -29, 0xFF, 0b1111_1111 (binary number)5**10**5 in irb)Float: 3.1415, 2.5e-10 (C double)String: "Aoyama", 'Aoyama'true, false, nil++, --**: Exponentiation+: Concatenation ('ab' + 'de'
⇒ 'abde')*: Repetition ('xyz' * 3 ⇒
'abde')<<: Appending (changes left side)[]: Extract substring/character%: For prinft-like formattingto_i"345".to_i ⇒ 345to_f
"3.14".to_f ⇒ 3.14to_s
[1,2,4,8].to_s ⇒ "[1, 2, 4, 8]"
(2.0/3).to_s ⇒ "0.6666666666666666"to_a, to_h,… also available-3.class ⇒Integer0.25.class ⇒ Float"Aoyama".class ⇒ String[1, 3, 5, 7, 9].class ⇒
Array{ 'true' => true, 'false' => false, 'nil' =>
false}.class ⇒ Hashclass Length
def initialize
@mm = 0.0
end
def mm; @mm; end
def in # inches
@mm * 25.4
end
def mm= milli
@mm = milli
end
def in= inch
@mm= inch / 25.4
end
end
Integer, String, Hash,
Length,…gcd, initialize, mm,
a, r@: instance variables@mm@@: class variables (not recommended)$: global variables (not recommended)=, ?, or
!puts or printputs and printgetsgets is dangerous, but in Ruby, it is safe)gets returns a single line, or nil if there is
no input anymore (EOF)while line = gets n = line.to_i puts "The square of " + n.to_s + " is " + (n*n).to_s end
main function, you just write what you
want Ruby to doputs "The square of #{n} is #{n*n}"#{} in a double-quoted string is called string
interpolation. It is very convenient and widely used in Ruby.to_japanese_calendar in a file
japanese_calendar.rb.Integer) in the Western calendar
as an argument.String indicating the year in the
Japanese calendar.requires
japanese_calendar.rb and uses your function. Use this program
to test your function.test_japanese_calendar.rb, and run the
tests withruby test_japanese_calendar.rbjapanese_calendar.rb to Moodle (deadline: today at 18:00).Temperature in a file
temperature.rb.Temperature represents a
temperature.celsius, fahrenheit,
kelvin) and setting (celsius=,
fahrenheit=, kelvin=).requires
temperature.rb and uses your class. Use this program to test
your function.test_temperature.rb, and run the tests
withruby test_temperature.rbtemperature.rb
to Moodle (deadline: Wednesday 22:00).Integer,
Float, String, Array (incl.
Enumerable), Hash